net: inet: Support UID-based routing
Subsystems (8)
PATCH 1/1 net: inet: Support UID-based routing
This patchset adds support for per-UID routing. It allows the administrator to configure rules such as:
ip rule add uidrange 100-200 lookup 123
This functionality has been in use by all Android devices since 5.0, which was released in late 2014. It is primarily used to impose per-app routing policies (on Android, every app has its own UID) without having to resort to rerouting packets in iptables, which breaks getsockname() and MTU/MSS calculation, and generally disrupts end-to-end connectivity.
This patch series is similar to the code currently used on Android, but has better correctness and performance because it stores the UID in the socket instead of calling sock_i_uid. This avoids contention on sk->sk_callback_lock, and makes it possible to correctly route a socket on which userspace has called close(), for which sock_i_uid will return 0.
PATCH 1/3 net: core: Add a UID field to struct sock.
Protocol sockets (struct sock) don't have UIDs, but most of the time, they map 1:1 to userspace sockets (struct socket) which do.
Various operations such as the iptables xt_owner match need access to the "UID of a socket", and do so by following the backpointer to the struct socket. This involves taking sk_callback_lock and doesn't work when there is no socket because userspace has already called close().
Simplify this by adding a sk_uid field to struct sock whose value matches the UID of the corresponding struct socket. The semantics are as follows:
1. Whenever sk_socket is non-null: sk_uid is the same as the UID in sk_socket, i.e., matches the return value of sock_i_uid. Specifically, the UID is set when userspace calls socket(), fchown(), or accept().
2. When sk_socket is null, sk_uid is defined as follows:
- For a socket that no longer has a sk_socket because userspace has called close(): the previous UID.
- For a cloned socket (e.g., an incoming connection that is established but on which userspace has not yet called accept): the UID of the socket it was cloned from.
- For a socket that has never had an sk_socket (e.g., a kernel ICMP socket): UID 0 inside the user namespace corresponding to the network namespace the socket belongs to. This is arguably better than GLOBAL_ROOT_UID because it allows "--owner-uid 0" to match kernel-originated packets in non-root namespaces.
--- include/net/sock.h | 7 +++++++ net/core/sock.c | 6 +++++- net/socket.c | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-)
include/net/sock.h +7 -0
@@ -417,6 +417,7 @@ struct sock {u32 sk_max_ack_backlog;__u32 sk_priority;__u32 sk_mark; +kuid_t sk_uid;struct pid *sk_peer_pid;const struct cred *sk_peer_cred;long sk_rcvtimeo;
@@ -1651,6 +1652,7 @@ static inline void sock_graft(struct sock *sk, struct socket *parent) sk->sk_wq = parent->wq; parent->sk = sk;sk_set_socket (sk, parent); + sk->sk_uid =SOCK_INODE (parent)->i_uid;security_sock_graft (sk, parent);write_unlock_bh (&sk->sk_callback_lock); }
@@ -1658,6 +1660,11 @@ static inline void sock_graft(struct sock *sk, struct socket *parent)kuid_t sock_i_uid (struct sock *sk);unsigned long sock_i_ino (struct sock *sk); +static inline kuid_t sock_net_uid (struct net *net,const struct sock *sk) +{ +return sk ? sk->sk_uid :make_kuid (net->user_ns,0 ); +} +static inline u32 net_tx_rndhash (void ) {u32 v =prandom_u32 ();
net/core/sock.c +5 -1
@@ -1570,6 +1570,7 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)*/ sk_refcnt_debug_inc (newsk);sk_set_socket (newsk,NULL ); + newsk->sk_uid = sk->sk_uid; newsk->sk_wq =NULL ;if (newsk->sk_prot->sockets_allocated)
@@ -2460,8 +2461,11 @@ void sock_init_data(struct socket *sock, struct sock *sk) sk->sk_type = sock->type; sk->sk_wq = sock->wq; sock->sk = sk; - }else + sk->sk_uid =SOCK_INODE (sock)->i_uid; + }else { sk->sk_wq =NULL ; + sk->sk_uid =make_kuid (sock_net (sk)->user_ns,0 ); + }rwlock_init (&sk->sk_callback_lock); lockdep_set_class_and_name(&sk->sk_callback_lock,
net/socket.c +14 -0
@@ -518,8 +518,22 @@ static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,return used; } +int sockfs_setattr (struct dentry *dentry,struct iattr *iattr) +{ +int err =simple_setattr (dentry, iattr); + +if (!err) { +struct socket *sock =SOCKET_I (d_inode (dentry)); + + sock->sk->sk_uid = iattr->ia_uid; + } + +return err; +} +static const struct inode_operations sockfs_inode_ops = { .listxattr = sockfs_listxattr, + .setattr = sockfs_setattr, };/**
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 2/3 net: core: add UID to flows, rules, and routes
In reply to Lorenzo Colitti- Define a new FIB rule attributes, FRA_UID_RANGE, to describe a range of UIDs.
- Define a RTA_UID attribute for per-UID route lookups and dumps.
- Support passing these attributes to and from userspace via rtnetlink. The value INVALID_UID indicates no UID was specified.
- Add a UID field to the flow structures.
--- include/net/fib_rules.h | 9 ++++- include/net/flow.h | 5 +++ include/uapi/linux/fib_rules.h | 6 ++++ include/uapi/linux/rtnetlink.h | 1 + net/core/fib_rules.c | 74 ++++++++++++++++++++++++++++++++++++++++-- net/ipv4/fib_frontend.c | 1 + net/ipv4/route.c | 11 +++++++ net/ipv6/route.c | 7 ++++ 8 files changed, 111 insertions(+), 3 deletions(-)
include/net/fib_rules.h +8 -1
@@ -8,6 +8,11 @@#include <net/flow.h> #include <net/rtnetlink.h> +struct fib_kuid_range { +kuid_t start; +kuid_t end; +}; +struct fib_rule {struct list_head list;int iifindex;
@@ -30,6 +35,7 @@ struct fib_rule {int suppress_prefixlen;char iifname[IFNAMSIZ ];char oifname[IFNAMSIZ ]; +struct fib_kuid_range uid_range;struct rcu_head rcu; };
@@ -92,7 +98,8 @@ struct fib_rules_ops { [FRA_SUPPRESS_PREFIXLEN] = { .type = NLA_U32 }, \ [FRA_SUPPRESS_IFGROUP] = { .type = NLA_U32 }, \ [FRA_GOTO] = { .type = NLA_U32 }, \ - [FRA_L3MDEV] = { .type = NLA_U8 } + [FRA_L3MDEV] = { .type = NLA_U8 }, \ + [FRA_UID_RANGE] = { .len = sizeof(struct fib_rule_uid_range) }static inline void fib_rule_get (struct fib_rule *rule) {
include/net/flow.h +5 -0
@@ -11,6 +11,7 @@#include <linux/in6.h> #include <linux/atomic.h> #include <net/flow_dissector.h> +#include <linux/uidgid.h> /* * ifindex generation is per-net namespace, and loopback is
@@ -37,6 +38,7 @@ struct flowi_common {#define FLOWI_FLAG_SKIP_NH_OIF 0x04__u32 flowic_secid;struct flowi_tunnel flowic_tun_key; +kuid_t flowic_uid; };union flowi_uli {
@@ -74,6 +76,7 @@ struct flowi4 {#define flowi4_flags __fl_common.flowic_flags#define flowi4_secid __fl_common.flowic_secid#define flowi4_tun_key __fl_common.flowic_tun_key +#define flowi4_uid __fl_common.flowic_uid/* (saddr,daddr) must be grouped, same order as in IP header */ __be32 saddr;
@@ -131,6 +134,7 @@ struct flowi6 {#define flowi6_flags __fl_common.flowic_flags#define flowi6_secid __fl_common.flowic_secid#define flowi6_tun_key __fl_common.flowic_tun_key +#define flowi6_uid __fl_common.flowic_uidstruct in6_addr daddr;struct in6_addr saddr;/* Note: flowi6_tos is encoded in flowlabel, too. */
@@ -176,6 +180,7 @@ struct flowi {#define flowi_flags u.__fl_common.flowic_flags#define flowi_secid u.__fl_common.flowic_secid#define flowi_tun_key u.__fl_common.flowic_tun_key +#define flowi_uid u.__fl_common.flowic_uid } __attribute__((__aligned__(BITS_PER_LONG/8)));static inline struct flowi *flowi4_to_flowi (struct flowi4 *fl4)
include/uapi/linux/fib_rules.h +6 -0
@@ -29,6 +29,11 @@ struct fib_rule_hdr {__u32 flags; }; +struct fib_rule_uid_range { +__u32 start; +__u32 end; +}; +enum {FRA_UNSPEC ,FRA_DST ,/* destination address */
@@ -51,6 +56,7 @@ enum {FRA_OIFNAME ,FRA_PAD ,FRA_L3MDEV ,/* iif or oif is l3mdev goto its table */ +FRA_UID_RANGE ,/* UID range */ __FRA_MAX };
include/uapi/linux/rtnetlink.h +1 -0
@@ -318,6 +318,7 @@ enum rtattr_type_t {RTA_ENCAP ,RTA_EXPIRES ,RTA_PAD , +RTA_UID , __RTA_MAX };
net/core/fib_rules.c +72 -2
134 lines not on this page · raw message
net/ipv4/fib_frontend.c +1 -0
@@ -610,6 +610,7 @@ const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = { [RTA_FLOW] = { .type = NLA_U32 }, [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, [RTA_ENCAP] = { .type = NLA_NESTED }, + [RTA_UID] = { .type = NLA_U32 }, }; static int rtm_to_fib_config(struct net *net, struct sk_buff *skb,
net/ipv4/route.c +11 -0
@@ -2493,6 +2493,11 @@ static int rt_fill_info(struct net *net, __be32 dst, __be32 src, u32 table_id, nla_put_u32(skb, RTA_MARK, fl4->flowi4_mark)) goto nla_put_failure; +if (!uid_eq (fl4->flowi4_uid,INVALID_UID ) && +nla_put_u32 (skb,RTA_UID , +from_kuid_munged (current_user_ns (), fl4->flowi4_uid))) + goto nla_put_failure; + error = rt->dst.error;if (rt_is_input_route (rt)) {
@@ -2545,6 +2550,7 @@ static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh)int mark;struct sk_buff *skb;u32 table_id =RT_TABLE_MAIN ; +kuid_t uid; err =nlmsg_parse (nlh,sizeof (*rtm), tb,RTA_MAX , rtm_ipv4_policy);if (err <0 )
@@ -2572,6 +2578,10 @@ static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh) dst = tb[RTA_DST ] ?nla_get_in_addr (tb[RTA_DST ]) :0 ; iif = tb[RTA_IIF ] ?nla_get_u32 (tb[RTA_IIF ]) :0 ; mark = tb[RTA_MARK ] ?nla_get_u32 (tb[RTA_MARK ]) :0 ; +if (tb[RTA_UID ]) + uid =make_kuid (current_user_ns (),nla_get_u32 (tb[RTA_UID ])); +else + uid = (iif ?INVALID_UID :current_uid ());memset (&fl4,0 ,sizeof (fl4)); fl4.daddr = dst;
@@ -2579,6 +2589,7 @@ static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh) fl4.flowi4_tos = rtm->rtm_tos; fl4.flowi4_oif = tb[RTA_OIF ] ?nla_get_u32 (tb[RTA_OIF ]) :0 ; fl4.flowi4_mark = mark; + fl4.flowi4_uid = uid;if (iif) {struct net_device *dev;
net/ipv6/route.c +7 -0
@@ -2767,6 +2767,7 @@ static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { [RTA_ENCAP_TYPE] = { .type = NLA_U16 }, [RTA_ENCAP] = { .type = NLA_NESTED }, [RTA_EXPIRES] = { .type = NLA_U32 }, + [RTA_UID] = { .type = NLA_U32 }, }; static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
@@ -3341,6 +3342,12 @@ static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh)if (tb[RTA_MARK ]) fl6.flowi6_mark =nla_get_u32 (tb[RTA_MARK ]); +if (tb[RTA_UID ]) + fl6.flowi6_uid =make_kuid (current_user_ns (), +nla_get_u32 (tb[RTA_UID ])); +else + fl6.flowi6_uid = iif ?INVALID_UID :current_uid (); +if (iif) {struct net_device *dev;int flags =0 ;
Signature · 1 line
2.8.0.rc3.226.g39d4020
PATCH 3/3 net: inet: Support UID-based routing in IP protocols.
In reply to Lorenzo Colitti- Use the UID in routing lookups made by protocol connect() and sendmsg() functions.
- Make sure that routing lookups triggered by incoming packets (e.g., Path MTU discovery) take the UID of the socket into account.
Tested: compiles allnoconfig, allyesconfig, allmodconfig
Tested: https://android-review.googlesource.com/253302
--- include/net/flow.h | 4 +++- include/net/ip.h | 1 + include/net/ip6_route.h | 2 +- include/net/route.h | 5 +++-- net/ipv4/icmp.c | 9 ++++++--- net/ipv4/inet_connection_sock.c | 4 ++-- net/ipv4/ip_output.c | 3 ++- net/ipv4/ping.c | 3 ++- net/ipv4/raw.c | 2 +- net/ipv4/route.c | 31 +++++++++++++++++-------------- net/ipv4/syncookies.c | 2 +- net/ipv4/tcp_ipv4.c | 1 + net/ipv4/udp.c | 3 ++- net/ipv6/af_inet6.c | 1 + net/ipv6/ah6.c | 2 +- net/ipv6/datagram.c | 1 + net/ipv6/esp6.c | 2 +- net/ipv6/icmp.c | 4 +++- net/ipv6/inet6_connection_sock.c | 2 ++ net/ipv6/ip6_gre.c | 4 ++++ net/ipv6/ip6_tunnel.c | 4 ++++ net/ipv6/ip6_vti.c | 3 ++- net/ipv6/ipcomp6.c | 2 +- net/ipv6/netfilter.c | 1 + net/ipv6/ping.c | 1 + net/ipv6/raw.c | 1 + net/ipv6/route.c | 7 +++++-- net/ipv6/syncookies.c | 1 + net/ipv6/tcp_ipv6.c | 2 ++ net/ipv6/udp.c | 1 + net/l2tp/l2tp_ip6.c | 1 + 31 files changed, 75 insertions(+), 35 deletions(-)
include/net/flow.h +3 -1
@@ -96,7 +96,8 @@ static inline void flowi4_init_output(struct flowi4 *fl4, int oif, __u32 mark, __u8 tos, __u8 scope, __u8 proto, __u8 flags, __be32 daddr, __be32 saddr, - __be16 dport, __be16 sport) + __be16 dport, __be16 sport, + kuid_t uid) { fl4->flowi4_oif = oif; fl4->flowi4_iif =LOOPBACK_IFINDEX ;
@@ -107,6 +108,7 @@ static inline void flowi4_init_output(struct flowi4 *fl4, int oif, fl4->flowi4_flags = flags; fl4->flowi4_secid =0 ; fl4->flowi4_tun_key.tun_id =0 ; + fl4->flowi4_uid = uid; fl4->daddr = daddr; fl4->saddr = saddr; fl4->fl4_dport = dport;
include/net/ip.h +1 -0
@@ -173,6 +173,7 @@ struct ip_reply_arg {/* -1 if not needed */ int bound_dev_if;u8 tos; +kuid_t uid; };#define IP_REPLY_ARG_NOSRCCHECK 1
include/net/ip6_route.h +1 -1
@@ -139,7 +139,7 @@ int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, const struct in6_addr *gwaddr);void ip6_update_pmtu (struct sk_buff *skb,struct net *net,__be32 mtu,int oif, -u32 mark); +u32 mark,kuid_t uid);void ip6_sk_update_pmtu (struct sk_buff *skb,struct sock *sk,__be32 mtu);void ip6_redirect (struct sk_buff *skb,struct net *net,int oif,u32 mark); void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
include/net/route.h +3 -2
@@ -153,7 +153,7 @@ static inline struct rtable *ip_route_output_ports(struct net *net, struct flowiflowi4_init_output (fl4, oif, sk ? sk->sk_mark :0 , tos,RT_SCOPE_UNIVERSE , proto, sk ?inet_sk_flowi_flags (sk) :0 , - daddr, saddr, dport, sport); + daddr, saddr, dport, sport,sock_net_uid (net, sk));if (sk)security_sk_classify_flow (sk,flowi4_to_flowi (fl4));return ip_route_output_flow (net, fl4, sk);
@@ -269,7 +269,8 @@ static inline void ip_route_connect_init(struct flowi4 *fl4, __be32 dst, __be32 flow_flags |=FLOWI_FLAG_ANYSRC ;flowi4_init_output (fl4, oif, sk->sk_mark, tos,RT_SCOPE_UNIVERSE , - protocol, flow_flags, dst, src, dport, sport); + protocol, flow_flags, dst, src, dport, sport, + sk->sk_uid); } static inline struct rtable *ip_route_connect(struct flowi4 *fl4,
net/ipv4/icmp.c +6 -3
@@ -425,6 +425,7 @@ static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb) fl4.daddr = daddr; fl4.saddr = saddr; fl4.flowi4_mark = mark; + fl4.flowi4_uid = sk->sk_uid; fl4.flowi4_tos =RT_TOS (ip_hdr (skb)->tos); fl4.flowi4_proto =IPPROTO_ICMP ; fl4.flowi4_oif =l3mdev_master_ifindex (skb->dev);
@@ -460,7 +461,8 @@ static struct rtable *icmp_route_lookup(struct net *net,struct flowi4 *fl4, struct sk_buff *skb_in, const struct iphdr *iph, - __be32 saddr, u8 tos, u32 mark, + __be32 saddr, u8 tos, + const struct sock *sk, int type, int code, struct icmp_bxm *param) {
@@ -472,7 +474,8 @@ static struct rtable *icmp_route_lookup(struct net *net, fl4->daddr = (param->replyopts.opt.opt.srr ? param->replyopts.opt.opt.faddr : iph->saddr); fl4->saddr = saddr; - fl4->flowi4_mark = mark; + fl4->flowi4_mark = sk->sk_mark; + fl4->flowi4_uid = sk->sk_uid; fl4->flowi4_tos =RT_TOS (tos); fl4->flowi4_proto =IPPROTO_ICMP ; fl4->fl4_icmp_type = type;
@@ -701,7 +704,7 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info) ipc.ttl =0 ; ipc.tos =-1 ; - rt =icmp_route_lookup (net, &fl4, skb_in, iph, saddr, tos, mark, + rt =icmp_route_lookup (net, &fl4, skb_in, iph, saddr, tos, sk, type, code, icmp_param);if (IS_ERR (rt)) goto out_unlock;
net/ipv4/inet_connection_sock.c +2 -2
@@ -415,7 +415,7 @@ struct dst_entry *inet_csk_route_req(const struct sock *sk, sk->sk_protocol,inet_sk_flowi_flags (sk), (opt && opt->opt.srr) ? opt->opt.faddr : ireq->ir_rmt_addr, ireq->ir_loc_addr, ireq->ir_rmt_port, - htons(ireq->ir_num)); + htons(ireq->ir_num), sk->sk_uid);security_req_classify_flow (req,flowi4_to_flowi (fl4)); rt =ip_route_output_flow (net, fl4, sk);if (IS_ERR (rt))
@@ -452,7 +452,7 @@ struct dst_entry *inet_csk_route_child_sock(const struct sock *sk, sk->sk_protocol,inet_sk_flowi_flags (sk), (opt && opt->opt.srr) ? opt->opt.faddr : ireq->ir_rmt_addr, ireq->ir_loc_addr, ireq->ir_rmt_port, - htons(ireq->ir_num)); + htons(ireq->ir_num), sk->sk_uid);security_req_classify_flow (req,flowi4_to_flowi (fl4)); rt =ip_route_output_flow (net, fl4, sk);if (IS_ERR (rt))
net/ipv4/ip_output.c +2 -1
@@ -1590,7 +1590,8 @@ void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb,RT_SCOPE_UNIVERSE ,ip_hdr (skb)->protocol,ip_reply_arg_flowi_flags (arg), daddr, saddr, - tcp_hdr(skb)->source, tcp_hdr(skb)->dest); +tcp_hdr (skb)->source,tcp_hdr (skb)->dest, + arg->uid);security_skb_classify_flow (skb,flowi4_to_flowi (&fl4)); rt =ip_route_output_key (net, &fl4);if (IS_ERR (rt))
net/ipv4/ping.c +2 -1
@@ -789,7 +789,8 @@ static int ping_v4_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)flowi4_init_output (&fl4, ipc.oif, sk->sk_mark, tos,RT_SCOPE_UNIVERSE , sk->sk_protocol, -inet_sk_flowi_flags (sk), faddr, saddr,0 ,0 ); +inet_sk_flowi_flags (sk), faddr, saddr,0 ,0 , + sk->sk_uid);security_sk_classify_flow (sk,flowi4_to_flowi (&fl4)); rt =ip_route_output_flow (net, &fl4, sk);
net/ipv4/raw.c +1 -1
@@ -606,7 +606,7 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) inet->hdrincl ?IPPROTO_RAW : sk->sk_protocol,inet_sk_flowi_flags (sk) | (inet->hdrincl ?FLOWI_FLAG_KNOWN_NH :0 ), - daddr, saddr, 0, 0); + daddr, saddr, 0, 0, sk->sk_uid);if (!inet->hdrincl) { rfv.msg = msg;
net/ipv4/route.c +17 -14
@@ -507,7 +507,8 @@ void __ip_select_ident(struct net *net, struct iphdr *iph, int segs) }EXPORT_SYMBOL (__ip_select_ident); -static void __build_flow_key (struct flowi4 *fl4,const struct sock *sk, +static void __build_flow_key (struct net *net,struct flowi4 *fl4, +struct sock *sk,const struct iphdr *iph,int oif,u8 tos,u8 prot,u32 mark,int flow_flags)
@@ -523,11 +524,11 @@ static void __build_flow_key(struct flowi4 *fl4, const struct sock *sk,flowi4_init_output (fl4, oif, mark, tos,RT_SCOPE_UNIVERSE , prot, flow_flags, - iph->daddr, iph->saddr,0 ,0 ); + iph->daddr, iph->saddr,0 ,0 , sk->sk_uid); }static void build_skb_flow_key (struct flowi4 *fl4,const struct sk_buff *skb, -const struct sock *sk) +struct sock *sk) {const struct iphdr *iph =ip_hdr (skb);int oif = skb->dev->ifindex;
@@ -535,10 +536,10 @@ static void build_skb_flow_key(struct flowi4 *fl4, const struct sk_buff *skb,u8 prot = iph->protocol;u32 mark = skb->mark; -__build_flow_key (fl4, sk, iph, oif, tos, prot, mark,0 ); +__build_flow_key (sock_net (sk), fl4, sk, iph, oif, tos, prot, mark,0 ); } -static void build_sk_flow_key (struct flowi4 *fl4,const struct sock *sk) +static void build_sk_flow_key (struct flowi4 *fl4,struct sock *sk) {const struct inet_sock *inet =inet_sk (sk);const struct ip_options_rcu *inet_opt;
@@ -552,11 +553,11 @@ static void build_sk_flow_key(struct flowi4 *fl4, const struct sock *sk)RT_CONN_FLAGS (sk),RT_SCOPE_UNIVERSE , inet->hdrincl ?IPPROTO_RAW : sk->sk_protocol,inet_sk_flowi_flags (sk), - daddr, inet->inet_saddr, 0, 0); + daddr, inet->inet_saddr, 0, 0, sk->sk_uid);rcu_read_unlock (); } -static void ip_rt_build_flow_key (struct flowi4 *fl4,const struct sock *sk, +static void ip_rt_build_flow_key (struct flowi4 *fl4,struct sock *sk,const struct sk_buff *skb) {if (skb)
@@ -800,7 +801,7 @@ static void ip_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buf rt = (struct rtable *) dst; -__build_flow_key (&fl4, sk, iph, oif, tos, prot, mark,0 ); +__build_flow_key (sock_net (sk), &fl4, sk, iph, oif, tos, prot, mark,0 );__ip_do_redirect (rt, skb, &fl4, true); }
@@ -1018,7 +1019,7 @@ void ipv4_update_pmtu(struct sk_buff *skb, struct net *net, u32 mtu,if (!mark) mark =IP4_REPLY_MARK (net, skb->mark); -__build_flow_key (&fl4,NULL , iph, oif, +__build_flow_key (net, &fl4,NULL , iph, oif,RT_TOS (iph->tos), protocol, mark, flow_flags); rt =__ip_route_output_key (net, &fl4);if (!IS_ERR (rt)) {
@@ -1034,7 +1035,7 @@ static void __ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)struct flowi4 fl4;struct rtable *rt; -__build_flow_key (&fl4, sk, iph,0 ,0 ,0 ,0 ,0 ); +__build_flow_key (sock_net (sk), &fl4, sk, iph,0 ,0 ,0 ,0 ,0 );if (!fl4.flowi4_mark) fl4.flowi4_mark =IP4_REPLY_MARK (sock_net (sk), skb->mark);
@@ -1053,6 +1054,7 @@ void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)struct rtable *rt;struct dst_entry *odst =NULL ;bool new = false; +struct net *net =sock_net (sk);bh_lock_sock (sk);
@@ -1066,7 +1068,7 @@ void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu) goto out; } -__build_flow_key (&fl4, sk, iph,0 ,0 ,0 ,0 ,0 ); +__build_flow_key (net, &fl4, sk, iph,0 ,0 ,0 ,0 ,0 ); rt = (struct rtable *)odst;if (odst->obsolete && !odst->ops->check (odst,0 )) {
@@ -1106,7 +1108,7 @@ void ipv4_redirect(struct sk_buff *skb, struct net *net,struct flowi4 fl4;struct rtable *rt; -__build_flow_key (&fl4,NULL , iph, oif, +__build_flow_key (net, &fl4,NULL , iph, oif,RT_TOS (iph->tos), protocol, mark, flow_flags); rt =__ip_route_output_key (net, &fl4);if (!IS_ERR (rt)) {
@@ -1121,9 +1123,10 @@ void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk)const struct iphdr *iph = (const struct iphdr *) skb->data;struct flowi4 fl4;struct rtable *rt; +struct net *net =sock_net (sk); -__build_flow_key (&fl4, sk, iph,0 ,0 ,0 ,0 ,0 ); - rt =__ip_route_output_key (sock_net (sk), &fl4); +__build_flow_key (net, &fl4, sk, iph,0 ,0 ,0 ,0 ,0 ); + rt =__ip_route_output_key (net, &fl4);if (!IS_ERR (rt)) {__ip_do_redirect (rt, skb, &fl4, false);ip_rt_put (rt);
net/ipv4/syncookies.c +1 -1
@@ -372,7 +372,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)RT_CONN_FLAGS (sk),RT_SCOPE_UNIVERSE ,IPPROTO_TCP ,inet_sk_flowi_flags (sk), opt->srr ? opt->faddr : ireq->ir_rmt_addr, - ireq->ir_loc_addr, th->source, th->dest); + ireq->ir_loc_addr, th->source, th->dest, sk->sk_uid);security_req_classify_flow (req,flowi4_to_flowi (&fl4)); rt =ip_route_output_key (sock_net (sk), &fl4);if (IS_ERR (rt)) {
net/ipv4/tcp_ipv4.c +1 -0
@@ -692,6 +692,7 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb) offsetof(struct inet_timewait_sock, tw_bound_dev_if)); arg.tos =ip_hdr (skb)->tos; + arg.uid =sock_net_uid (net, sk);local_bh_disable (); ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk), skb, &TCP_SKB_CB(skb)->header.h4.opt,
net/ipv4/udp.c +2 -1
@@ -1019,7 +1019,8 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)flowi4_init_output (fl4, ipc.oif, sk->sk_mark, tos,RT_SCOPE_UNIVERSE , sk->sk_protocol, flow_flags, - faddr, saddr, dport, inet->inet_sport); + faddr, saddr, dport, inet->inet_sport, + sk->sk_uid);security_sk_classify_flow (sk,flowi4_to_flowi (fl4)); rt =ip_route_output_flow (net, fl4, sk);
net/ipv6/af_inet6.c +1 -0
@@ -678,6 +678,7 @@ int inet6_sk_rebuild_header(struct sock *sk) fl6.flowi6_mark = sk->sk_mark; fl6.fl6_dport = inet->inet_dport; fl6.fl6_sport = inet->inet_sport; + fl6.flowi6_uid = sk->sk_uid;security_sk_classify_flow (sk,flowi6_to_flowi (&fl6));rcu_read_lock ();
net/ipv6/ah6.c +1 -1
@@ -664,7 +664,7 @@ static int ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,if (type ==NDISC_REDIRECT )ip6_redirect (skb, net, skb->dev->ifindex,0 );else -ip6_update_pmtu (skb, net, info,0 ,0 ); +ip6_update_pmtu (skb, net, info,0 ,0 ,sock_net_uid (net,NULL ));xfrm_state_put (x);return 0 ;
net/ipv6/datagram.c +1 -0
@@ -54,6 +54,7 @@ static void ip6_datagram_flow_key_init(struct flowi6 *fl6, struct sock *sk) fl6->fl6_dport = inet->inet_dport; fl6->fl6_sport = inet->inet_sport; fl6->flowlabel = np->flow_label; + fl6->flowi6_uid = sk->sk_uid;if (!fl6->flowi6_oif) fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
net/ipv6/esp6.c +1 -1
@@ -476,7 +476,7 @@ static int esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,if (type ==NDISC_REDIRECT )ip6_redirect (skb, net, skb->dev->ifindex,0 );else -ip6_update_pmtu (skb, net, info,0 ,0 ); +ip6_update_pmtu (skb, net, info,0 ,0 ,sock_net_uid (net,NULL ));xfrm_state_put (x);return 0 ;
net/ipv6/icmp.c +3 -1
@@ -92,7 +92,7 @@ static void icmpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,struct net *net =dev_net (skb->dev);if (type ==ICMPV6_PKT_TOOBIG ) -ip6_update_pmtu (skb, net, info,0 ,0 ); +ip6_update_pmtu (skb, net, info,0 ,0 ,sock_net_uid (net,NULL ));else if (type ==NDISC_REDIRECT )ip6_redirect (skb, net, skb->dev->ifindex,0 );
@@ -507,6 +507,7 @@ static void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info, ipc6.tclass = np->tclass; fl6.flowlabel =ip6_make_flowinfo (ipc6.tclass, fl6.flowlabel); + fl6.flowi6_uid = sk->sk_uid; dst =icmpv6_route_lookup (net, skb, sk, &fl6);if (IS_ERR (dst))
@@ -670,6 +671,7 @@ static void icmpv6_echo_reply(struct sk_buff *skb) fl6.flowi6_oif = np->mcast_oif; else if (!fl6.flowi6_oif) fl6.flowi6_oif = np->ucast_oif; + fl6.flowi6_uid =sock_net_uid (net,NULL ); err =ip6_dst_lookup (net, sk, &dst, &fl6);if (err)
net/ipv6/inet6_connection_sock.c +2 -0
@@ -88,6 +88,7 @@ struct dst_entry *inet6_csk_route_req(const struct sock *sk, fl6->flowi6_mark = ireq->ir_mark; fl6->fl6_dport = ireq->ir_rmt_port; fl6->fl6_sport =htons (ireq->ir_num); + fl6->flowi6_uid = sk->sk_uid;security_req_classify_flow (req,flowi6_to_flowi (fl6)); dst =ip6_dst_lookup_flow (sk, fl6, final_p);
@@ -136,6 +137,7 @@ static struct dst_entry *inet6_csk_route_socket(struct sock *sk, fl6->flowi6_mark = sk->sk_mark; fl6->fl6_sport = inet->inet_sport; fl6->fl6_dport = inet->inet_dport; + fl6->flowi6_uid = sk->sk_uid;security_sk_classify_flow (sk,flowi6_to_flowi (fl6));rcu_read_lock ();
net/ipv6/ip6_gre.c +4 -0
@@ -548,6 +548,8 @@ static inline int ip6gre_xmit_ipv4(struct sk_buff *skb, struct net_device *dev)if (t->parms.flags &IP6_TNL_F_USE_ORIG_FWMARK ) fl6.flowi6_mark = skb->mark; + fl6.flowi6_uid =sock_net_uid (dev_net (dev),NULL ); + err =gre_handle_offloads (skb, !!(t->parms.o_flags &TUNNEL_CSUM ));if (err)return -1 ;
@@ -602,6 +604,8 @@ static inline int ip6gre_xmit_ipv6(struct sk_buff *skb, struct net_device *dev)if (t->parms.flags &IP6_TNL_F_USE_ORIG_FWMARK ) fl6.flowi6_mark = skb->mark; + fl6.flowi6_uid =sock_net_uid (dev_net (dev),NULL ); +if (gre_handle_offloads (skb, !!(t->parms.o_flags &TUNNEL_CSUM )))return -1 ;
net/ipv6/ip6_tunnel.c +4 -0
@@ -1237,6 +1237,8 @@ ip4ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) fl6.flowi6_mark = skb->mark; } + fl6.flowi6_uid =sock_net_uid (dev_net (dev),NULL ); +if (iptunnel_handle_offloads (skb,SKB_GSO_IPXIP6 ))return -1 ;
@@ -1315,6 +1317,8 @@ ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) fl6.flowi6_mark = skb->mark; } + fl6.flowi6_uid =sock_net_uid (dev_net (dev),NULL ); +if (iptunnel_handle_offloads (skb,SKB_GSO_IPXIP6 ))return -1 ;
net/ipv6/ip6_vti.c +2 -1
@@ -610,7 +610,8 @@ static int vti6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,if (type ==NDISC_REDIRECT )ip6_redirect (skb, net, skb->dev->ifindex,0 );else -ip6_update_pmtu (skb, net, info,0 ,0 ); +ip6_update_pmtu (skb, net, info,0 ,0 , +sock_net_uid (net, skb->sk));xfrm_state_put (x);return 0 ;
net/ipv6/ipcomp6.c +1 -1
@@ -76,7 +76,7 @@ static int ipcomp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,if (type ==NDISC_REDIRECT )ip6_redirect (skb, net, skb->dev->ifindex,0 );else -ip6_update_pmtu (skb, net, info,0 ,0 ); +ip6_update_pmtu (skb, net, info,0 ,0 ,sock_net_uid (net,NULL ));xfrm_state_put (x);return 0 ;
net/ipv6/netfilter.c +1 -0
@@ -26,6 +26,7 @@ int ip6_route_me_harder(struct net *net, struct sk_buff *skb)struct flowi6 fl6 = { .flowi6_oif = skb->sk ? skb->sk->sk_bound_dev_if :0 , .flowi6_mark = skb->mark, + .flowi6_uid =sock_net_uid (net, skb->sk), .daddr = iph->daddr, .saddr = iph->saddr, };
net/ipv6/ping.c +1 -0
@@ -113,6 +113,7 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) fl6.daddr = *daddr; fl6.flowi6_oif = oif; fl6.flowi6_mark = sk->sk_mark; + fl6.flowi6_uid = sk->sk_uid; fl6.fl6_icmp_type = user_icmph.icmp6_type; fl6.fl6_icmp_code = user_icmph.icmp6_code;security_sk_classify_flow (sk,flowi6_to_flowi (&fl6));
net/ipv6/raw.c +1 -0
@@ -776,6 +776,7 @@ static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)memset (&fl6,0 ,sizeof (fl6)); fl6.flowi6_mark = sk->sk_mark; + fl6.flowi6_uid = sk->sk_uid; ipc6.hlimit =-1 ; ipc6.tclass =-1 ;
net/ipv6/route.c +5 -2
@@ -1401,7 +1401,7 @@ static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, }void ip6_update_pmtu (struct sk_buff *skb,struct net *net,__be32 mtu, -int oif,u32 mark) +int oif,u32 mark,kuid_t uid) {const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data;struct dst_entry *dst;
@@ -1413,6 +1413,7 @@ void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, fl6.daddr = iph->daddr; fl6.saddr = iph->saddr; fl6.flowlabel =ip6_flowinfo (iph); + fl6.flowi6_uid = uid; dst =ip6_route_output (net,NULL , &fl6);if (!dst->error)
@@ -1426,7 +1427,7 @@ void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu)struct dst_entry *dst;ip6_update_pmtu (skb,sock_net (sk), mtu, - sk->sk_bound_dev_if, sk->sk_mark); + sk->sk_bound_dev_if, sk->sk_mark, sk->sk_uid); dst =__sk_dst_get (sk); if (!dst || !dst->obsolete ||
@@ -1531,6 +1532,7 @@ void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark) fl6.daddr = iph->daddr; fl6.saddr = iph->saddr; fl6.flowlabel =ip6_flowinfo (iph); + fl6.flowi6_uid =sock_net_uid (net,NULL ); dst =ip6_route_redirect (net, &fl6, &ipv6_hdr (skb)->saddr);rt6_do_redirect (dst,NULL , skb);
@@ -1552,6 +1554,7 @@ void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif, fl6.flowi6_mark = mark; fl6.daddr = msg->dest; fl6.saddr = iph->daddr; + fl6.flowi6_uid =sock_net_uid (net,NULL ); dst =ip6_route_redirect (net, &fl6, &iph->saddr);rt6_do_redirect (dst,NULL , skb);
net/ipv6/syncookies.c +1 -0
@@ -227,6 +227,7 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb) fl6.flowi6_mark = ireq->ir_mark; fl6.fl6_dport = ireq->ir_rmt_port; fl6.fl6_sport =inet_sk (sk)->inet_sport; + fl6.flowi6_uid = sk->sk_uid;security_req_classify_flow (req,flowi6_to_flowi (&fl6)); dst =ip6_dst_lookup_flow (sk, &fl6, final_p);
net/ipv6/tcp_ipv6.c +2 -0
@@ -233,6 +233,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr, fl6.flowi6_mark = sk->sk_mark; fl6.fl6_dport = usin->sin6_port; fl6.fl6_sport = inet->inet_sport; + fl6.flowi6_uid = sk->sk_uid; opt =rcu_dereference_protected (np->opt,lockdep_sock_is_held (sk)); final_p =fl6_update_dst (&fl6, opt, &final);
@@ -824,6 +825,7 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32 fl6.flowi6_mark =IP6_REPLY_MARK (net, skb->mark); fl6.fl6_dport = t1->dest; fl6.fl6_sport = t1->source; + fl6.flowi6_uid =sock_net_uid (net, (struct sock *)sk);security_skb_classify_flow (skb,flowi6_to_flowi (&fl6));/* Pass a socket to ip6_dst_lookup either it is for RST
net/ipv6/udp.c +1 -0
@@ -1137,6 +1137,7 @@ int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex; fl6.flowi6_mark = sk->sk_mark; + fl6.flowi6_uid = sk->sk_uid; sockc.tsflags = sk->sk_tsflags;if (msg->msg_controllen) {
net/l2tp/l2tp_ip6.c +1 -0
@@ -519,6 +519,7 @@ static int l2tp_ip6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)memset (&fl6,0 ,sizeof (fl6)); fl6.flowi6_mark = sk->sk_mark; + fl6.flowi6_uid = sk->sk_uid; ipc6.hlimit =-1 ; ipc6.tclass =-1 ;
Signature · 1 line
2.8.0.rc3.226.g39d4020
Re: [PATCH net-next 1/3] net: core: Add a UID field to struct sock.
In reply to Lorenzo Colittinet/core/sock.c struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority) · line 1573
Quoted 9 lines · Lorenzo Colitti
net/core/sock.c +1
-0@@ -1570,6 +1570,7 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)*/ sk_refcnt_debug_inc (newsk);sk_set_socket (newsk,NULL ); + newsk->sk_uid = sk->sk_uid;
Not sure why this is needed ?
sk_clone_lock() already copies sk to newsk, using sock_copy(newsk, sk);
Quoted 4 lines
net/core/sock.c +0
-0newsk->sk_wq =NULL ;if (newsk->sk_prot->sockets_allocated)@@ -2460,8 +2461,11 @@ void sock_init_data(struct socket *sock, struct sock *sk)
kbuild test robot
build botRe: [PATCH net-next 1/3] net: core: Add a UID field to struct sock.
In reply to Lorenzo Colittiurl: https://github.com/0day-ci/linux/commits/Lorenzo-Colitti/net-core-Add-a-UID-field-to-struct-sock/20161028-020552 reproduce: make htmldocs
Hi Lorenzo,
[auto build test WARNING on net-next/master]
url: https://github.com/0day-ci/linux/commits/Lorenzo-Colitti/net-core-Add-a-UID-field-to-struct-sock/20161028-020552
reproduce: make htmldocs
All warnings (new ones prefixed by >>):
include/linux/skbuff.h:947: warning: No description found for parameter 'sk' include/net/sock.h:450: warning: No description found for parameter 'sk_padding'
>> include/net/sock.h:450: warning: No description found for parameter 'sk_uid'
include/net/sock.h:450: warning: No description found for parameter 'sk_rcu' include/linux/netdevice.h:1902: warning: No description found for parameter 'prio_tc_map[TC_BITMASK + 1]'
vim +/sk_uid +450 include/net/sock.h
ef64a54f6 Pavel Emelyanov 2012-02-21 434 __s32 sk_peek_off; ^1da177e4 Linus Torvalds 2005-04-16 435 int sk_write_pending; d5f642384 Alexey Dobriyan 2008-11-04 436 #ifdef CONFIG_SECURITY ^1da177e4 Linus Torvalds 2005-04-16 437 void *sk_security; d5f642384 Alexey Dobriyan 2008-11-04 438 #endif 2a56a1fec Tejun Heo 2015-12-07 439 struct sock_cgroup_data sk_cgrp_data; baac50bbc Johannes Weiner 2016-01-14 440 struct mem_cgroup *sk_memcg; ^1da177e4 Linus Torvalds 2005-04-16 441 void (*sk_state_change)(struct sock *sk); 676d23690 David S. Miller 2014-04-11 442 void (*sk_data_ready)(struct sock *sk); ^1da177e4 Linus Torvalds 2005-04-16 443 void (*sk_write_space)(struct sock *sk); ^1da177e4 Linus Torvalds 2005-04-16 444 void (*sk_error_report)(struct sock *sk); ^1da177e4 Linus Torvalds 2005-04-16 445 int (*sk_backlog_rcv)(struct sock *sk, ^1da177e4 Linus Torvalds 2005-04-16 446 struct sk_buff *skb); ^1da177e4 Linus Torvalds 2005-04-16 447 void (*sk_destruct)(struct sock *sk); ef456144d Craig Gallek 2016-01-04 448 struct sock_reuseport __rcu *sk_reuseport_cb; a4298e452 Eric Dumazet 2016-04-01 449 struct rcu_head sk_rcu; ^1da177e4 Linus Torvalds 2005-04-16 @450 }; ^1da177e4 Linus Torvalds 2005-04-16 451 559835ea7 Pravin B Shelar 2013-09-24 452 #define __sk_user_data(sk) ((*((void __rcu **)&(sk)->sk_user_data))) 559835ea7 Pravin B Shelar 2013-09-24 453 559835ea7 Pravin B Shelar 2013-09-24 454 #define rcu_dereference_sk_user_data(sk) rcu_dereference(__sk_user_data((sk))) 559835ea7 Pravin B Shelar 2013-09-24 455 #define rcu_assign_sk_user_data(sk, ptr) rcu_assign_pointer(__sk_user_data((sk)), ptr) 559835ea7 Pravin B Shelar 2013-09-24 456 4a17fd522 Pavel Emelyanov 2012-04-19 457 /* 4a17fd522 Pavel Emelyanov 2012-04-19 458 * SK_CAN_REUSE and SK_NO_REUSE on a socket mean that the socket is OK
:::::: The code at line 450 was first introduced by commit :::::: 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 Linux-2.6.12-rc2
:::::: TO: Linus Torvalds <[email protected]> :::::: CC: Linus Torvalds <[email protected]>
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
David Miller
maintainerRe: [PATCH net-next 1/3] net: core: Add a UID field to struct sock.
In reply to Eric DumazetFrom: Eric Dumazet <[email protected]>
Date: Thu, 27 Oct 2016 10:36:26 -0700
Quoted 14 lines · Lorenzo Colitti
net/core/sock.c struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority) · line 1573
Quoted 10 lines
net/core/sock.c +1
-0@@ -1570,6 +1570,7 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)*/ sk_refcnt_debug_inc (newsk);sk_set_socket (newsk,NULL ); + newsk->sk_uid = sk->sk_uid;Not sure why this is needed ?
Quoted 1 line
sk_clone_lock()already copies sk to newsk, usingsock_copy(newsk, sk);
Agreed.