blob: 772d7389b3376d623c9ab3a6ce71883619e356e1 [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>
17#include <linux/netfilter/x_tables.h>
18#include <linux/netfilter/xt_owner.h>
Jan Engelhardt0265ab42007-12-04 23:27:38 -080019
20static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020021owner_mt(const struct sk_buff *skb, struct xt_action_param *par)
Jan Engelhardt0265ab42007-12-04 23:27:38 -080022{
Jan Engelhardtf7108a22008-10-08 11:35:18 +020023 const struct xt_owner_match_info *info = par->matchinfo;
Jan Engelhardt0265ab42007-12-04 23:27:38 -080024 const struct file *filp;
25
26 if (skb->sk == NULL || skb->sk->sk_socket == NULL)
27 return (info->match ^ info->invert) == 0;
28 else if (info->match & info->invert & XT_OWNER_SOCKET)
29 /*
30 * Socket exists but user wanted ! --socket-exists.
31 * (Single ampersands intended.)
32 */
33 return false;
34
35 filp = skb->sk->sk_socket->file;
36 if (filp == NULL)
37 return ((info->match ^ info->invert) &
38 (XT_OWNER_UID | XT_OWNER_GID)) == 0;
39
40 if (info->match & XT_OWNER_UID)
David Howellsd76b0d92008-11-14 10:39:25 +110041 if ((filp->f_cred->fsuid >= info->uid_min &&
42 filp->f_cred->fsuid <= info->uid_max) ^
Jan Engelhardtedc26f72008-01-31 04:06:38 -080043 !(info->invert & XT_OWNER_UID))
Jan Engelhardt0265ab42007-12-04 23:27:38 -080044 return false;
45
46 if (info->match & XT_OWNER_GID)
David Howellsd76b0d92008-11-14 10:39:25 +110047 if ((filp->f_cred->fsgid >= info->gid_min &&
48 filp->f_cred->fsgid <= info->gid_max) ^
Jan Engelhardtedc26f72008-01-31 04:06:38 -080049 !(info->invert & XT_OWNER_GID))
Jan Engelhardt0265ab42007-12-04 23:27:38 -080050 return false;
51
52 return true;
53}
54
Jan Engelhardt6461cae2009-06-12 19:46:26 +020055static struct xt_match owner_mt_reg __read_mostly = {
56 .name = "owner",
57 .revision = 1,
58 .family = NFPROTO_UNSPEC,
59 .match = owner_mt,
60 .matchsize = sizeof(struct xt_owner_match_info),
61 .hooks = (1 << NF_INET_LOCAL_OUT) |
62 (1 << NF_INET_POST_ROUTING),
63 .me = THIS_MODULE,
Jan Engelhardt0265ab42007-12-04 23:27:38 -080064};
65
66static int __init owner_mt_init(void)
67{
Jan Engelhardt6461cae2009-06-12 19:46:26 +020068 return xt_register_match(&owner_mt_reg);
Jan Engelhardt0265ab42007-12-04 23:27:38 -080069}
70
71static void __exit owner_mt_exit(void)
72{
Jan Engelhardt6461cae2009-06-12 19:46:26 +020073 xt_unregister_match(&owner_mt_reg);
Jan Engelhardt0265ab42007-12-04 23:27:38 -080074}
75
76module_init(owner_mt_init);
77module_exit(owner_mt_exit);
Jan Engelhardt6461cae2009-06-12 19:46:26 +020078MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080079MODULE_DESCRIPTION("Xtables: socket owner matching");
Jan Engelhardt0265ab42007-12-04 23:27:38 -080080MODULE_LICENSE("GPL");
81MODULE_ALIAS("ipt_owner");
82MODULE_ALIAS("ip6t_owner");