]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/netfilter/xt_owner.c
Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[mirror_ubuntu-artful-kernel.git] / net / netfilter / xt_owner.c
CommitLineData
0265ab44
JE
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 *
edc26f7a 7 * Copyright © CC Computer Consultants GmbH, 2007 - 2008
0265ab44
JE
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>
5b825c3a
IM
16#include <linux/cred.h>
17
0265ab44 18#include <net/sock.h>
fdd723e2 19#include <net/inet_sock.h>
0265ab44
JE
20#include <linux/netfilter/x_tables.h>
21#include <linux/netfilter/xt_owner.h>
0265ab44 22
26711a79
EB
23static int owner_check(const struct xt_mtchk_param *par)
24{
25 struct xt_owner_match_info *info = par->matchinfo;
9847371a 26 struct net *net = par->net;
26711a79 27
9847371a
EB
28 /* Only allow the common case where the userns of the writer
29 * matches the userns of the network namespace.
30 */
26711a79 31 if ((info->match & (XT_OWNER_UID|XT_OWNER_GID)) &&
9847371a 32 (current_user_ns() != net->user_ns))
26711a79 33 return -EINVAL;
9847371a
EB
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
26711a79
EB
59 return 0;
60}
61
0265ab44 62static bool
62fc8051 63owner_mt(const struct sk_buff *skb, struct xt_action_param *par)
0265ab44 64{
f7108a20 65 const struct xt_owner_match_info *info = par->matchinfo;
0265ab44 66 const struct file *filp;
fdd723e2 67 struct sock *sk = skb_to_full_sk(skb);
613dbd95 68 struct net *net = xt_net(par);
0265ab44 69
fdd723e2 70 if (sk == NULL || sk->sk_socket == NULL)
0265ab44
JE
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
fdd723e2 79 filp = sk->sk_socket->file;
0265ab44
JE
80 if (filp == NULL)
81 return ((info->match ^ info->invert) &
82 (XT_OWNER_UID | XT_OWNER_GID)) == 0;
83
26711a79 84 if (info->match & XT_OWNER_UID) {
9847371a
EB
85 kuid_t uid_min = make_kuid(net->user_ns, info->uid_min);
86 kuid_t uid_max = make_kuid(net->user_ns, info->uid_max);
26711a79
EB
87 if ((uid_gte(filp->f_cred->fsuid, uid_min) &&
88 uid_lte(filp->f_cred->fsuid, uid_max)) ^
edc26f7a 89 !(info->invert & XT_OWNER_UID))
0265ab44 90 return false;
26711a79 91 }
0265ab44 92
26711a79 93 if (info->match & XT_OWNER_GID) {
9847371a
EB
94 kgid_t gid_min = make_kgid(net->user_ns, info->gid_min);
95 kgid_t gid_max = make_kgid(net->user_ns, info->gid_max);
26711a79
EB
96 if ((gid_gte(filp->f_cred->fsgid, gid_min) &&
97 gid_lte(filp->f_cred->fsgid, gid_max)) ^
edc26f7a 98 !(info->invert & XT_OWNER_GID))
0265ab44 99 return false;
26711a79 100 }
0265ab44
JE
101
102 return true;
103}
104
6461caed
JE
105static struct xt_match owner_mt_reg __read_mostly = {
106 .name = "owner",
107 .revision = 1,
108 .family = NFPROTO_UNSPEC,
26711a79 109 .checkentry = owner_check,
6461caed
JE
110 .match = owner_mt,
111 .matchsize = sizeof(struct xt_owner_match_info),
112 .hooks = (1 << NF_INET_LOCAL_OUT) |
113 (1 << NF_INET_POST_ROUTING),
114 .me = THIS_MODULE,
0265ab44
JE
115};
116
117static int __init owner_mt_init(void)
118{
6461caed 119 return xt_register_match(&owner_mt_reg);
0265ab44
JE
120}
121
122static void __exit owner_mt_exit(void)
123{
6461caed 124 xt_unregister_match(&owner_mt_reg);
0265ab44
JE
125}
126
127module_init(owner_mt_init);
128module_exit(owner_mt_exit);
6461caed 129MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
2ae15b64 130MODULE_DESCRIPTION("Xtables: socket owner matching");
0265ab44
JE
131MODULE_LICENSE("GPL");
132MODULE_ALIAS("ipt_owner");
133MODULE_ALIAS("ip6t_owner");