Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at master 171 lines 4.7 kB view raw
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 struct sock *sk = skb_to_full_sk(skb); 67 struct net *net = xt_net(par); 68 const struct socket *sock; 69 const struct file *filp; 70 71 if (!sk || !READ_ONCE(sk->sk_socket) || !net_eq(net, sock_net(sk))) 72 return (info->match ^ info->invert) == 0; 73 else if (info->match & info->invert & XT_OWNER_SOCKET) 74 /* 75 * Socket exists but user wanted ! --socket-exists. 76 * (Single ampersands intended.) 77 */ 78 return false; 79 80 /* The sk pointer remains valid as long as the skb is. The sk_socket and 81 * file pointer may become NULL if the socket is closed. Both structures 82 * (including file->cred) are RCU freed which means they can be accessed 83 * within a RCU read section. 84 */ 85 sock = READ_ONCE(sk->sk_socket); 86 filp = sock ? READ_ONCE(sock->file) : NULL; 87 if (filp == NULL) 88 return ((info->match ^ info->invert) & 89 (XT_OWNER_UID | XT_OWNER_GID)) == 0; 90 91 if (info->match & XT_OWNER_UID) { 92 kuid_t uid_min = make_kuid(net->user_ns, info->uid_min); 93 kuid_t uid_max = make_kuid(net->user_ns, info->uid_max); 94 95 if ((uid_gte(filp->f_cred->fsuid, uid_min) && 96 uid_lte(filp->f_cred->fsuid, uid_max)) ^ 97 !(info->invert & XT_OWNER_UID)) 98 return false; 99 } 100 101 if (info->match & XT_OWNER_GID) { 102 unsigned int i, match = false; 103 kgid_t gid_min = make_kgid(net->user_ns, info->gid_min); 104 kgid_t gid_max = make_kgid(net->user_ns, info->gid_max); 105 struct group_info *gi = filp->f_cred->group_info; 106 107 if (gid_gte(filp->f_cred->fsgid, gid_min) && 108 gid_lte(filp->f_cred->fsgid, gid_max)) 109 match = true; 110 111 if (!match && (info->match & XT_OWNER_SUPPL_GROUPS) && gi) { 112 for (i = 0; i < gi->ngroups; ++i) { 113 kgid_t group = gi->gid[i]; 114 115 if (gid_gte(group, gid_min) && 116 gid_lte(group, gid_max)) { 117 match = true; 118 break; 119 } 120 } 121 } 122 123 if (match ^ !(info->invert & XT_OWNER_GID)) 124 return false; 125 } 126 127 return true; 128} 129 130static struct xt_match owner_mt_reg[] __read_mostly = { 131 { 132 .name = "owner", 133 .revision = 1, 134 .family = NFPROTO_IPV4, 135 .checkentry = owner_check, 136 .match = owner_mt, 137 .matchsize = sizeof(struct xt_owner_match_info), 138 .hooks = (1 << NF_INET_LOCAL_OUT) | 139 (1 << NF_INET_POST_ROUTING), 140 .me = THIS_MODULE, 141 }, 142 { 143 .name = "owner", 144 .revision = 1, 145 .family = NFPROTO_IPV6, 146 .checkentry = owner_check, 147 .match = owner_mt, 148 .matchsize = sizeof(struct xt_owner_match_info), 149 .hooks = (1 << NF_INET_LOCAL_OUT) | 150 (1 << NF_INET_POST_ROUTING), 151 .me = THIS_MODULE, 152 } 153}; 154 155static int __init owner_mt_init(void) 156{ 157 return xt_register_matches(owner_mt_reg, ARRAY_SIZE(owner_mt_reg)); 158} 159 160static void __exit owner_mt_exit(void) 161{ 162 xt_unregister_matches(owner_mt_reg, ARRAY_SIZE(owner_mt_reg)); 163} 164 165module_init(owner_mt_init); 166module_exit(owner_mt_exit); 167MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>"); 168MODULE_DESCRIPTION("Xtables: socket owner matching"); 169MODULE_LICENSE("GPL"); 170MODULE_ALIAS("ipt_owner"); 171MODULE_ALIAS("ip6t_owner");