blob: a20e731b5b6c79a01e0762405932ed0685fbe002 [file] [log] [blame]
Jan Engelhardt0265ab42007-12-04 23:27:38 -08001/*
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 Engelhardtedc26f72008-01-31 04:06:38 -08007 * Copyright © CC Computer Consultants GmbH, 2007 - 2008
Jan Engelhardt0265ab42007-12-04 23:27:38 -08008 *
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 Dumazetfdd723e2015-11-08 10:54:09 -080017#include <net/inet_sock.h>
Jan Engelhardt0265ab42007-12-04 23:27:38 -080018#include <linux/netfilter/x_tables.h>
19#include <linux/netfilter/xt_owner.h>
Jan Engelhardt0265ab42007-12-04 23:27:38 -080020
Eric W. Biederman26711a72012-02-02 17:33:59 -080021static int owner_check(const struct xt_mtchk_param *par)
22{
23 struct xt_owner_match_info *info = par->matchinfo;
Eric W. Biederman98473712016-06-14 15:14:12 -070024 struct net *net = par->net;
Eric W. Biederman26711a72012-02-02 17:33:59 -080025
Eric W. Biederman98473712016-06-14 15:14:12 -070026 /* Only allow the common case where the userns of the writer
27 * matches the userns of the network namespace.
28 */
Eric W. Biederman26711a72012-02-02 17:33:59 -080029 if ((info->match & (XT_OWNER_UID|XT_OWNER_GID)) &&
Eric W. Biederman98473712016-06-14 15:14:12 -070030 (current_user_ns() != net->user_ns))
Eric W. Biederman26711a72012-02-02 17:33:59 -080031 return -EINVAL;
Eric W. Biederman98473712016-06-14 15:14:12 -070032
33 /* Ensure the uids are valid */
34 if (info->match & XT_OWNER_UID) {
35 kuid_t uid_min = make_kuid(net->user_ns, info->uid_min);
36 kuid_t uid_max = make_kuid(net->user_ns, info->uid_max);
37
38 if (!uid_valid(uid_min) || !uid_valid(uid_max) ||
39 (info->uid_max < info->uid_min) ||
40 uid_lt(uid_max, uid_min)) {
41 return -EINVAL;
42 }
43 }
44
45 /* Ensure the gids are valid */
46 if (info->match & XT_OWNER_GID) {
47 kgid_t gid_min = make_kgid(net->user_ns, info->gid_min);
48 kgid_t gid_max = make_kgid(net->user_ns, info->gid_max);
49
50 if (!gid_valid(gid_min) || !gid_valid(gid_max) ||
51 (info->gid_max < info->gid_min) ||
52 gid_lt(gid_max, gid_min)) {
53 return -EINVAL;
54 }
55 }
56
Eric W. Biederman26711a72012-02-02 17:33:59 -080057 return 0;
58}
59
Jan Engelhardt0265ab42007-12-04 23:27:38 -080060static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020061owner_mt(const struct sk_buff *skb, struct xt_action_param *par)
Jan Engelhardt0265ab42007-12-04 23:27:38 -080062{
Jan Engelhardtf7108a22008-10-08 11:35:18 +020063 const struct xt_owner_match_info *info = par->matchinfo;
Jan Engelhardt0265ab42007-12-04 23:27:38 -080064 const struct file *filp;
Eric Dumazetfdd723e2015-11-08 10:54:09 -080065 struct sock *sk = skb_to_full_sk(skb);
Eric W. Biederman98473712016-06-14 15:14:12 -070066 struct net *net = par->net;
Jan Engelhardt0265ab42007-12-04 23:27:38 -080067
Eric Dumazetfdd723e2015-11-08 10:54:09 -080068 if (sk == NULL || sk->sk_socket == NULL)
Jan Engelhardt0265ab42007-12-04 23:27:38 -080069 return (info->match ^ info->invert) == 0;
70 else if (info->match & info->invert & XT_OWNER_SOCKET)
71 /*
72 * Socket exists but user wanted ! --socket-exists.
73 * (Single ampersands intended.)
74 */
75 return false;
76
Eric Dumazetfdd723e2015-11-08 10:54:09 -080077 filp = sk->sk_socket->file;
Jan Engelhardt0265ab42007-12-04 23:27:38 -080078 if (filp == NULL)
79 return ((info->match ^ info->invert) &
80 (XT_OWNER_UID | XT_OWNER_GID)) == 0;
81
Eric W. Biederman26711a72012-02-02 17:33:59 -080082 if (info->match & XT_OWNER_UID) {
Eric W. Biederman98473712016-06-14 15:14:12 -070083 kuid_t uid_min = make_kuid(net->user_ns, info->uid_min);
84 kuid_t uid_max = make_kuid(net->user_ns, info->uid_max);
Eric W. Biederman26711a72012-02-02 17:33:59 -080085 if ((uid_gte(filp->f_cred->fsuid, uid_min) &&
86 uid_lte(filp->f_cred->fsuid, uid_max)) ^
Jan Engelhardtedc26f72008-01-31 04:06:38 -080087 !(info->invert & XT_OWNER_UID))
Jan Engelhardt0265ab42007-12-04 23:27:38 -080088 return false;
Eric W. Biederman26711a72012-02-02 17:33:59 -080089 }
Jan Engelhardt0265ab42007-12-04 23:27:38 -080090
Eric W. Biederman26711a72012-02-02 17:33:59 -080091 if (info->match & XT_OWNER_GID) {
Eric W. Biederman98473712016-06-14 15:14:12 -070092 kgid_t gid_min = make_kgid(net->user_ns, info->gid_min);
93 kgid_t gid_max = make_kgid(net->user_ns, info->gid_max);
Eric W. Biederman26711a72012-02-02 17:33:59 -080094 if ((gid_gte(filp->f_cred->fsgid, gid_min) &&
95 gid_lte(filp->f_cred->fsgid, gid_max)) ^
Jan Engelhardtedc26f72008-01-31 04:06:38 -080096 !(info->invert & XT_OWNER_GID))
Jan Engelhardt0265ab42007-12-04 23:27:38 -080097 return false;
Eric W. Biederman26711a72012-02-02 17:33:59 -080098 }
Jan Engelhardt0265ab42007-12-04 23:27:38 -080099
100 return true;
101}
102
Jan Engelhardt6461cae2009-06-12 19:46:26 +0200103static struct xt_match owner_mt_reg __read_mostly = {
104 .name = "owner",
105 .revision = 1,
106 .family = NFPROTO_UNSPEC,
Eric W. Biederman26711a72012-02-02 17:33:59 -0800107 .checkentry = owner_check,
Jan Engelhardt6461cae2009-06-12 19:46:26 +0200108 .match = owner_mt,
109 .matchsize = sizeof(struct xt_owner_match_info),
110 .hooks = (1 << NF_INET_LOCAL_OUT) |
111 (1 << NF_INET_POST_ROUTING),
112 .me = THIS_MODULE,
Jan Engelhardt0265ab42007-12-04 23:27:38 -0800113};
114
115static int __init owner_mt_init(void)
116{
Jan Engelhardt6461cae2009-06-12 19:46:26 +0200117 return xt_register_match(&owner_mt_reg);
Jan Engelhardt0265ab42007-12-04 23:27:38 -0800118}
119
120static void __exit owner_mt_exit(void)
121{
Jan Engelhardt6461cae2009-06-12 19:46:26 +0200122 xt_unregister_match(&owner_mt_reg);
Jan Engelhardt0265ab42007-12-04 23:27:38 -0800123}
124
125module_init(owner_mt_init);
126module_exit(owner_mt_exit);
Jan Engelhardt6461cae2009-06-12 19:46:26 +0200127MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -0800128MODULE_DESCRIPTION("Xtables: socket owner matching");
Jan Engelhardt0265ab42007-12-04 23:27:38 -0800129MODULE_LICENSE("GPL");
130MODULE_ALIAS("ipt_owner");
131MODULE_ALIAS("ip6t_owner");