1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kernel module to match various things tied to sockets associated with
4 * locally generated outgoing packets.
6 * (C) 2000 Marc Boucher <marc@mbsi.ca>
8 * Copyright © CC Computer Consultants GmbH, 2007 - 2008
10 #include <linux/module.h>
11 #include <linux/skbuff.h>
12 #include <linux/file.h>
13 #include <linux/cred.h>
16 #include <net/inet_sock.h>
17 #include <linux/netfilter/x_tables.h>
18 #include <linux/netfilter/xt_owner.h>
20 static int owner_check(const struct xt_mtchk_param
*par
)
22 struct xt_owner_match_info
*info
= par
->matchinfo
;
23 struct net
*net
= par
->net
;
25 if (info
->match
& ~XT_OWNER_MASK
)
28 /* Only allow the common case where the userns of the writer
29 * matches the userns of the network namespace.
31 if ((info
->match
& (XT_OWNER_UID
|XT_OWNER_GID
)) &&
32 (current_user_ns() != net
->user_ns
))
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
);
40 if (!uid_valid(uid_min
) || !uid_valid(uid_max
) ||
41 (info
->uid_max
< info
->uid_min
) ||
42 uid_lt(uid_max
, uid_min
)) {
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
);
52 if (!gid_valid(gid_min
) || !gid_valid(gid_max
) ||
53 (info
->gid_max
< info
->gid_min
) ||
54 gid_lt(gid_max
, gid_min
)) {
63 owner_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
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
);
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
)
74 * Socket exists but user wanted ! --socket-exists.
75 * (Single ampersands intended.)
79 read_lock_bh(&sk
->sk_callback_lock
);
80 filp
= sk
->sk_socket
? sk
->sk_socket
->file
: NULL
;
82 read_unlock_bh(&sk
->sk_callback_lock
);
83 return ((info
->match
^ info
->invert
) &
84 (XT_OWNER_UID
| XT_OWNER_GID
)) == 0;
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
);
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
;
104 if (gid_gte(filp
->f_cred
->fsgid
, gid_min
) &&
105 gid_lte(filp
->f_cred
->fsgid
, gid_max
))
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
];
112 if (gid_gte(group
, gid_min
) &&
113 gid_lte(group
, gid_max
)) {
120 if (match
^ !(info
->invert
& XT_OWNER_GID
)) {
121 read_unlock_bh(&sk
->sk_callback_lock
);
126 read_unlock_bh(&sk
->sk_callback_lock
);
130 static struct xt_match owner_mt_reg __read_mostly
= {
133 .family
= NFPROTO_UNSPEC
,
134 .checkentry
= owner_check
,
136 .matchsize
= sizeof(struct xt_owner_match_info
),
137 .hooks
= (1 << NF_INET_LOCAL_OUT
) |
138 (1 << NF_INET_POST_ROUTING
),
142 static int __init
owner_mt_init(void)
144 return xt_register_match(&owner_mt_reg
);
147 static void __exit
owner_mt_exit(void)
149 xt_unregister_match(&owner_mt_reg
);
152 module_init(owner_mt_init
);
153 module_exit(owner_mt_exit
);
154 MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
155 MODULE_DESCRIPTION("Xtables: socket owner matching");
156 MODULE_LICENSE("GPL");
157 MODULE_ALIAS("ipt_owner");
158 MODULE_ALIAS("ip6t_owner");