Linux Audio

Check our new training course

Loading...
v4.10.11
 
  1/*
  2 * Kernel module to match various things tied to sockets associated with
  3 * locally generated outgoing packets.
  4 *
  5 * (C) 2000 Marc Boucher <marc@mbsi.ca>
  6 *
  7 * Copyright © CC Computer Consultants GmbH, 2007 - 2008
  8 *
  9 * This program is free software; you can redistribute it and/or modify
 10 * it under the terms of the GNU General Public License version 2 as
 11 * published by the Free Software Foundation.
 12 */
 13#include <linux/module.h>
 14#include <linux/skbuff.h>
 15#include <linux/file.h>
 
 
 16#include <net/sock.h>
 17#include <net/inet_sock.h>
 18#include <linux/netfilter/x_tables.h>
 19#include <linux/netfilter/xt_owner.h>
 20
 21static int owner_check(const struct xt_mtchk_param *par)
 22{
 23	struct xt_owner_match_info *info = par->matchinfo;
 24	struct net *net = par->net;
 25
 
 
 
 26	/* Only allow the common case where the userns of the writer
 27	 * matches the userns of the network namespace.
 28	 */
 29	if ((info->match & (XT_OWNER_UID|XT_OWNER_GID)) &&
 30	    (current_user_ns() != net->user_ns))
 31		return -EINVAL;
 32
 33	/* Ensure the uids are valid */
 34	if (info->match & XT_OWNER_UID) {
 35		kuid_t uid_min = make_kuid(net->user_ns, info->uid_min);
 36		kuid_t uid_max = make_kuid(net->user_ns, info->uid_max);
 37
 38		if (!uid_valid(uid_min) || !uid_valid(uid_max) ||
 39		    (info->uid_max < info->uid_min) ||
 40		    uid_lt(uid_max, uid_min)) {
 41			return -EINVAL;
 42		}
 43	}
 44
 45	/* Ensure the gids are valid */
 46	if (info->match & XT_OWNER_GID) {
 47		kgid_t gid_min = make_kgid(net->user_ns, info->gid_min);
 48		kgid_t gid_max = make_kgid(net->user_ns, info->gid_max);
 49
 50		if (!gid_valid(gid_min) || !gid_valid(gid_max) ||
 51		    (info->gid_max < info->gid_min) ||
 52		    gid_lt(gid_max, gid_min)) {
 53			return -EINVAL;
 54		}
 55	}
 56
 57	return 0;
 58}
 59
 60static bool
 61owner_mt(const struct sk_buff *skb, struct xt_action_param *par)
 62{
 63	const struct xt_owner_match_info *info = par->matchinfo;
 64	const struct file *filp;
 65	struct sock *sk = skb_to_full_sk(skb);
 66	struct net *net = xt_net(par);
 67
 68	if (sk == NULL || sk->sk_socket == NULL)
 69		return (info->match ^ info->invert) == 0;
 70	else if (info->match & info->invert & XT_OWNER_SOCKET)
 71		/*
 72		 * Socket exists but user wanted ! --socket-exists.
 73		 * (Single ampersands intended.)
 74		 */
 75		return false;
 76
 77	filp = sk->sk_socket->file;
 78	if (filp == NULL)
 
 
 79		return ((info->match ^ info->invert) &
 80		       (XT_OWNER_UID | XT_OWNER_GID)) == 0;
 
 81
 82	if (info->match & XT_OWNER_UID) {
 83		kuid_t uid_min = make_kuid(net->user_ns, info->uid_min);
 84		kuid_t uid_max = make_kuid(net->user_ns, info->uid_max);
 85		if ((uid_gte(filp->f_cred->fsuid, uid_min) &&
 86		     uid_lte(filp->f_cred->fsuid, uid_max)) ^
 87		    !(info->invert & XT_OWNER_UID))
 
 88			return false;
 
 89	}
 90
 91	if (info->match & XT_OWNER_GID) {
 
 92		kgid_t gid_min = make_kgid(net->user_ns, info->gid_min);
 93		kgid_t gid_max = make_kgid(net->user_ns, info->gid_max);
 94		if ((gid_gte(filp->f_cred->fsgid, gid_min) &&
 95		     gid_lte(filp->f_cred->fsgid, gid_max)) ^
 96		    !(info->invert & XT_OWNER_GID))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 97			return false;
 
 98	}
 99
 
100	return true;
101}
102
103static struct xt_match owner_mt_reg __read_mostly = {
104	.name       = "owner",
105	.revision   = 1,
106	.family     = NFPROTO_UNSPEC,
107	.checkentry = owner_check,
108	.match      = owner_mt,
109	.matchsize  = sizeof(struct xt_owner_match_info),
110	.hooks      = (1 << NF_INET_LOCAL_OUT) |
111	              (1 << NF_INET_POST_ROUTING),
112	.me         = THIS_MODULE,
113};
114
115static int __init owner_mt_init(void)
116{
117	return xt_register_match(&owner_mt_reg);
118}
119
120static void __exit owner_mt_exit(void)
121{
122	xt_unregister_match(&owner_mt_reg);
123}
124
125module_init(owner_mt_init);
126module_exit(owner_mt_exit);
127MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
128MODULE_DESCRIPTION("Xtables: socket owner matching");
129MODULE_LICENSE("GPL");
130MODULE_ALIAS("ipt_owner");
131MODULE_ALIAS("ip6t_owner");
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Kernel module to match various things tied to sockets associated with
  4 * locally generated outgoing packets.
  5 *
  6 * (C) 2000 Marc Boucher <marc@mbsi.ca>
  7 *
  8 * Copyright © CC Computer Consultants GmbH, 2007 - 2008
 
 
 
 
  9 */
 10#include <linux/module.h>
 11#include <linux/skbuff.h>
 12#include <linux/file.h>
 13#include <linux/cred.h>
 14
 15#include <net/sock.h>
 16#include <net/inet_sock.h>
 17#include <linux/netfilter/x_tables.h>
 18#include <linux/netfilter/xt_owner.h>
 19
 20static int owner_check(const struct xt_mtchk_param *par)
 21{
 22	struct xt_owner_match_info *info = par->matchinfo;
 23	struct net *net = par->net;
 24
 25	if (info->match & ~XT_OWNER_MASK)
 26		return -EINVAL;
 27
 28	/* Only allow the common case where the userns of the writer
 29	 * matches the userns of the network namespace.
 30	 */
 31	if ((info->match & (XT_OWNER_UID|XT_OWNER_GID)) &&
 32	    (current_user_ns() != net->user_ns))
 33		return -EINVAL;
 34
 35	/* Ensure the uids are valid */
 36	if (info->match & XT_OWNER_UID) {
 37		kuid_t uid_min = make_kuid(net->user_ns, info->uid_min);
 38		kuid_t uid_max = make_kuid(net->user_ns, info->uid_max);
 39
 40		if (!uid_valid(uid_min) || !uid_valid(uid_max) ||
 41		    (info->uid_max < info->uid_min) ||
 42		    uid_lt(uid_max, uid_min)) {
 43			return -EINVAL;
 44		}
 45	}
 46
 47	/* Ensure the gids are valid */
 48	if (info->match & XT_OWNER_GID) {
 49		kgid_t gid_min = make_kgid(net->user_ns, info->gid_min);
 50		kgid_t gid_max = make_kgid(net->user_ns, info->gid_max);
 51
 52		if (!gid_valid(gid_min) || !gid_valid(gid_max) ||
 53		    (info->gid_max < info->gid_min) ||
 54		    gid_lt(gid_max, gid_min)) {
 55			return -EINVAL;
 56		}
 57	}
 58
 59	return 0;
 60}
 61
 62static bool
 63owner_mt(const struct sk_buff *skb, struct xt_action_param *par)
 64{
 65	const struct xt_owner_match_info *info = par->matchinfo;
 66	const struct file *filp;
 67	struct sock *sk = skb_to_full_sk(skb);
 68	struct net *net = xt_net(par);
 69
 70	if (!sk || !sk->sk_socket || !net_eq(net, sock_net(sk)))
 71		return (info->match ^ info->invert) == 0;
 72	else if (info->match & info->invert & XT_OWNER_SOCKET)
 73		/*
 74		 * Socket exists but user wanted ! --socket-exists.
 75		 * (Single ampersands intended.)
 76		 */
 77		return false;
 78
 79	read_lock_bh(&sk->sk_callback_lock);
 80	filp = sk->sk_socket ? sk->sk_socket->file : NULL;
 81	if (filp == NULL) {
 82		read_unlock_bh(&sk->sk_callback_lock);
 83		return ((info->match ^ info->invert) &
 84		       (XT_OWNER_UID | XT_OWNER_GID)) == 0;
 85	}
 86
 87	if (info->match & XT_OWNER_UID) {
 88		kuid_t uid_min = make_kuid(net->user_ns, info->uid_min);
 89		kuid_t uid_max = make_kuid(net->user_ns, info->uid_max);
 90		if ((uid_gte(filp->f_cred->fsuid, uid_min) &&
 91		     uid_lte(filp->f_cred->fsuid, uid_max)) ^
 92		    !(info->invert & XT_OWNER_UID)) {
 93			read_unlock_bh(&sk->sk_callback_lock);
 94			return false;
 95		}
 96	}
 97
 98	if (info->match & XT_OWNER_GID) {
 99		unsigned int i, match = false;
100		kgid_t gid_min = make_kgid(net->user_ns, info->gid_min);
101		kgid_t gid_max = make_kgid(net->user_ns, info->gid_max);
102		struct group_info *gi = filp->f_cred->group_info;
103
104		if (gid_gte(filp->f_cred->fsgid, gid_min) &&
105		    gid_lte(filp->f_cred->fsgid, gid_max))
106			match = true;
107
108		if (!match && (info->match & XT_OWNER_SUPPL_GROUPS) && gi) {
109			for (i = 0; i < gi->ngroups; ++i) {
110				kgid_t group = gi->gid[i];
111
112				if (gid_gte(group, gid_min) &&
113				    gid_lte(group, gid_max)) {
114					match = true;
115					break;
116				}
117			}
118		}
119
120		if (match ^ !(info->invert & XT_OWNER_GID)) {
121			read_unlock_bh(&sk->sk_callback_lock);
122			return false;
123		}
124	}
125
126	read_unlock_bh(&sk->sk_callback_lock);
127	return true;
128}
129
130static struct xt_match owner_mt_reg __read_mostly = {
131	.name       = "owner",
132	.revision   = 1,
133	.family     = NFPROTO_UNSPEC,
134	.checkentry = owner_check,
135	.match      = owner_mt,
136	.matchsize  = sizeof(struct xt_owner_match_info),
137	.hooks      = (1 << NF_INET_LOCAL_OUT) |
138	              (1 << NF_INET_POST_ROUTING),
139	.me         = THIS_MODULE,
140};
141
142static int __init owner_mt_init(void)
143{
144	return xt_register_match(&owner_mt_reg);
145}
146
147static void __exit owner_mt_exit(void)
148{
149	xt_unregister_match(&owner_mt_reg);
150}
151
152module_init(owner_mt_init);
153module_exit(owner_mt_exit);
154MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
155MODULE_DESCRIPTION("Xtables: socket owner matching");
156MODULE_LICENSE("GPL");
157MODULE_ALIAS("ipt_owner");
158MODULE_ALIAS("ip6t_owner");