Jan Engelhardt | 0265ab4 | 2007-12-04 23:27:38 -0800 | [diff] [blame] | 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 | * |
Jan Engelhardt | edc26f7 | 2008-01-31 04:06:38 -0800 | [diff] [blame] | 7 | * Copyright © CC Computer Consultants GmbH, 2007 - 2008 |
Jan Engelhardt | 0265ab4 | 2007-12-04 23:27:38 -0800 | [diff] [blame] | 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> |
Eric Dumazet | fdd723e | 2015-11-08 10:54:09 -0800 | [diff] [blame] | 17 | #include <net/inet_sock.h> |
Jan Engelhardt | 0265ab4 | 2007-12-04 23:27:38 -0800 | [diff] [blame] | 18 | #include <linux/netfilter/x_tables.h> |
| 19 | #include <linux/netfilter/xt_owner.h> |
Jan Engelhardt | 0265ab4 | 2007-12-04 23:27:38 -0800 | [diff] [blame] | 20 | |
Eric W. Biederman | 26711a7 | 2012-02-02 17:33:59 -0800 | [diff] [blame] | 21 | static int owner_check(const struct xt_mtchk_param *par) |
| 22 | { |
| 23 | struct xt_owner_match_info *info = par->matchinfo; |
| 24 | |
| 25 | /* For now only allow adding matches from the initial user namespace */ |
| 26 | if ((info->match & (XT_OWNER_UID|XT_OWNER_GID)) && |
| 27 | (current_user_ns() != &init_user_ns)) |
| 28 | return -EINVAL; |
| 29 | return 0; |
| 30 | } |
| 31 | |
Jan Engelhardt | 0265ab4 | 2007-12-04 23:27:38 -0800 | [diff] [blame] | 32 | static bool |
Jan Engelhardt | 62fc805 | 2009-07-07 20:42:08 +0200 | [diff] [blame] | 33 | owner_mt(const struct sk_buff *skb, struct xt_action_param *par) |
Jan Engelhardt | 0265ab4 | 2007-12-04 23:27:38 -0800 | [diff] [blame] | 34 | { |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 35 | const struct xt_owner_match_info *info = par->matchinfo; |
Jan Engelhardt | 0265ab4 | 2007-12-04 23:27:38 -0800 | [diff] [blame] | 36 | const struct file *filp; |
Eric Dumazet | fdd723e | 2015-11-08 10:54:09 -0800 | [diff] [blame] | 37 | struct sock *sk = skb_to_full_sk(skb); |
Jan Engelhardt | 0265ab4 | 2007-12-04 23:27:38 -0800 | [diff] [blame] | 38 | |
Eric Dumazet | fdd723e | 2015-11-08 10:54:09 -0800 | [diff] [blame] | 39 | if (sk == NULL || sk->sk_socket == NULL) |
Jan Engelhardt | 0265ab4 | 2007-12-04 23:27:38 -0800 | [diff] [blame] | 40 | return (info->match ^ info->invert) == 0; |
| 41 | else if (info->match & info->invert & XT_OWNER_SOCKET) |
| 42 | /* |
| 43 | * Socket exists but user wanted ! --socket-exists. |
| 44 | * (Single ampersands intended.) |
| 45 | */ |
| 46 | return false; |
| 47 | |
Eric Dumazet | fdd723e | 2015-11-08 10:54:09 -0800 | [diff] [blame] | 48 | filp = sk->sk_socket->file; |
Jan Engelhardt | 0265ab4 | 2007-12-04 23:27:38 -0800 | [diff] [blame] | 49 | if (filp == NULL) |
| 50 | return ((info->match ^ info->invert) & |
| 51 | (XT_OWNER_UID | XT_OWNER_GID)) == 0; |
| 52 | |
Eric W. Biederman | 26711a7 | 2012-02-02 17:33:59 -0800 | [diff] [blame] | 53 | if (info->match & XT_OWNER_UID) { |
| 54 | kuid_t uid_min = make_kuid(&init_user_ns, info->uid_min); |
| 55 | kuid_t uid_max = make_kuid(&init_user_ns, info->uid_max); |
| 56 | if ((uid_gte(filp->f_cred->fsuid, uid_min) && |
| 57 | uid_lte(filp->f_cred->fsuid, uid_max)) ^ |
Jan Engelhardt | edc26f7 | 2008-01-31 04:06:38 -0800 | [diff] [blame] | 58 | !(info->invert & XT_OWNER_UID)) |
Jan Engelhardt | 0265ab4 | 2007-12-04 23:27:38 -0800 | [diff] [blame] | 59 | return false; |
Eric W. Biederman | 26711a7 | 2012-02-02 17:33:59 -0800 | [diff] [blame] | 60 | } |
Jan Engelhardt | 0265ab4 | 2007-12-04 23:27:38 -0800 | [diff] [blame] | 61 | |
Eric W. Biederman | 26711a7 | 2012-02-02 17:33:59 -0800 | [diff] [blame] | 62 | if (info->match & XT_OWNER_GID) { |
| 63 | kgid_t gid_min = make_kgid(&init_user_ns, info->gid_min); |
| 64 | kgid_t gid_max = make_kgid(&init_user_ns, info->gid_max); |
| 65 | if ((gid_gte(filp->f_cred->fsgid, gid_min) && |
| 66 | gid_lte(filp->f_cred->fsgid, gid_max)) ^ |
Jan Engelhardt | edc26f7 | 2008-01-31 04:06:38 -0800 | [diff] [blame] | 67 | !(info->invert & XT_OWNER_GID)) |
Jan Engelhardt | 0265ab4 | 2007-12-04 23:27:38 -0800 | [diff] [blame] | 68 | return false; |
Eric W. Biederman | 26711a7 | 2012-02-02 17:33:59 -0800 | [diff] [blame] | 69 | } |
Jan Engelhardt | 0265ab4 | 2007-12-04 23:27:38 -0800 | [diff] [blame] | 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
Jan Engelhardt | 6461cae | 2009-06-12 19:46:26 +0200 | [diff] [blame] | 74 | static struct xt_match owner_mt_reg __read_mostly = { |
| 75 | .name = "owner", |
| 76 | .revision = 1, |
| 77 | .family = NFPROTO_UNSPEC, |
Eric W. Biederman | 26711a7 | 2012-02-02 17:33:59 -0800 | [diff] [blame] | 78 | .checkentry = owner_check, |
Jan Engelhardt | 6461cae | 2009-06-12 19:46:26 +0200 | [diff] [blame] | 79 | .match = owner_mt, |
| 80 | .matchsize = sizeof(struct xt_owner_match_info), |
| 81 | .hooks = (1 << NF_INET_LOCAL_OUT) | |
| 82 | (1 << NF_INET_POST_ROUTING), |
| 83 | .me = THIS_MODULE, |
Jan Engelhardt | 0265ab4 | 2007-12-04 23:27:38 -0800 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | static int __init owner_mt_init(void) |
| 87 | { |
Jan Engelhardt | 6461cae | 2009-06-12 19:46:26 +0200 | [diff] [blame] | 88 | return xt_register_match(&owner_mt_reg); |
Jan Engelhardt | 0265ab4 | 2007-12-04 23:27:38 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static void __exit owner_mt_exit(void) |
| 92 | { |
Jan Engelhardt | 6461cae | 2009-06-12 19:46:26 +0200 | [diff] [blame] | 93 | xt_unregister_match(&owner_mt_reg); |
Jan Engelhardt | 0265ab4 | 2007-12-04 23:27:38 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | module_init(owner_mt_init); |
| 97 | module_exit(owner_mt_exit); |
Jan Engelhardt | 6461cae | 2009-06-12 19:46:26 +0200 | [diff] [blame] | 98 | MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 99 | MODULE_DESCRIPTION("Xtables: socket owner matching"); |
Jan Engelhardt | 0265ab4 | 2007-12-04 23:27:38 -0800 | [diff] [blame] | 100 | MODULE_LICENSE("GPL"); |
| 101 | MODULE_ALIAS("ipt_owner"); |
| 102 | MODULE_ALIAS("ip6t_owner"); |