127823fa8baec862704cc6e9577509680e763691
[sdk/emulator/emulator-kernel.git] / net / netfilter / xt_owner.c
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
21 static int owner_check(const struct xt_mtchk_param *par)
22 {
23         struct xt_owner_match_info *info = par->matchinfo;
24
25         if (info->match & ~XT_OWNER_MASK)
26                 return -EINVAL;
27
28         /* For now only allow adding matches from the initial user namespace */
29         if ((info->match & (XT_OWNER_UID|XT_OWNER_GID)) &&
30             (current_user_ns() != &init_user_ns))
31                 return -EINVAL;
32         return 0;
33 }
34
35 static bool
36 owner_mt(const struct sk_buff *skb, struct xt_action_param *par)
37 {
38         const struct xt_owner_match_info *info = par->matchinfo;
39         const struct file *filp;
40         struct sock *sk = skb_to_full_sk(skb);
41
42         if (sk == NULL || sk->sk_socket == NULL)
43                 return (info->match ^ info->invert) == 0;
44         else if (info->match & info->invert & XT_OWNER_SOCKET)
45                 /*
46                  * Socket exists but user wanted ! --socket-exists.
47                  * (Single ampersands intended.)
48                  */
49                 return false;
50
51         filp = sk->sk_socket->file;
52         if (filp == NULL)
53                 return ((info->match ^ info->invert) &
54                        (XT_OWNER_UID | XT_OWNER_GID)) == 0;
55
56         if (info->match & XT_OWNER_UID) {
57                 kuid_t uid_min = make_kuid(&init_user_ns, info->uid_min);
58                 kuid_t uid_max = make_kuid(&init_user_ns, info->uid_max);
59                 if ((uid_gte(filp->f_cred->fsuid, uid_min) &&
60                      uid_lte(filp->f_cred->fsuid, uid_max)) ^
61                     !(info->invert & XT_OWNER_UID))
62                         return false;
63         }
64
65         if (info->match & XT_OWNER_GID) {
66                 kgid_t gid_min = make_kgid(&init_user_ns, info->gid_min);
67                 kgid_t gid_max = make_kgid(&init_user_ns, info->gid_max);
68                 if ((gid_gte(filp->f_cred->fsgid, gid_min) &&
69                      gid_lte(filp->f_cred->fsgid, gid_max)) ^
70                     !(info->invert & XT_OWNER_GID))
71                         return false;
72         }
73
74         return true;
75 }
76
77 static struct xt_match owner_mt_reg __read_mostly = {
78         .name       = "owner",
79         .revision   = 1,
80         .family     = NFPROTO_UNSPEC,
81         .checkentry = owner_check,
82         .match      = owner_mt,
83         .matchsize  = sizeof(struct xt_owner_match_info),
84         .hooks      = (1 << NF_INET_LOCAL_OUT) |
85                       (1 << NF_INET_POST_ROUTING),
86         .me         = THIS_MODULE,
87 };
88
89 static int __init owner_mt_init(void)
90 {
91         return xt_register_match(&owner_mt_reg);
92 }
93
94 static void __exit owner_mt_exit(void)
95 {
96         xt_unregister_match(&owner_mt_reg);
97 }
98
99 module_init(owner_mt_init);
100 module_exit(owner_mt_exit);
101 MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
102 MODULE_DESCRIPTION("Xtables: socket owner matching");
103 MODULE_LICENSE("GPL");
104 MODULE_ALIAS("ipt_owner");
105 MODULE_ALIAS("ip6t_owner");