@@ -99,6 +99,7 @@ static size_t inet_sk_attr_size(void)
9999 + nla_total_size (1 ) /* INET_DIAG_SHUTDOWN */
100100 + nla_total_size (1 ) /* INET_DIAG_TOS */
101101 + nla_total_size (1 ) /* INET_DIAG_TCLASS */
102+ + nla_total_size (4 ) /* INET_DIAG_MARK */
102103 + nla_total_size (sizeof (struct inet_diag_meminfo ))
103104 + nla_total_size (sizeof (struct inet_diag_msg ))
104105 + nla_total_size (SK_MEMINFO_VARS * sizeof (u32 ))
@@ -109,7 +110,8 @@ static size_t inet_sk_attr_size(void)
109110
110111int inet_diag_msg_attrs_fill (struct sock * sk , struct sk_buff * skb ,
111112 struct inet_diag_msg * r , int ext ,
112- struct user_namespace * user_ns )
113+ struct user_namespace * user_ns ,
114+ bool net_admin )
113115{
114116 const struct inet_sock * inet = inet_sk (sk );
115117
@@ -136,6 +138,9 @@ int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
136138 }
137139#endif
138140
141+ if (net_admin && nla_put_u32 (skb , INET_DIAG_MARK , sk -> sk_mark ))
142+ goto errout ;
143+
139144 r -> idiag_uid = from_kuid_munged (user_ns , sock_i_uid (sk ));
140145 r -> idiag_inode = sock_i_ino (sk );
141146
@@ -149,7 +154,8 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
149154 struct sk_buff * skb , const struct inet_diag_req_v2 * req ,
150155 struct user_namespace * user_ns ,
151156 u32 portid , u32 seq , u16 nlmsg_flags ,
152- const struct nlmsghdr * unlh )
157+ const struct nlmsghdr * unlh ,
158+ bool net_admin )
153159{
154160 const struct tcp_congestion_ops * ca_ops ;
155161 const struct inet_diag_handler * handler ;
@@ -175,7 +181,7 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
175181 r -> idiag_timer = 0 ;
176182 r -> idiag_retrans = 0 ;
177183
178- if (inet_diag_msg_attrs_fill (sk , skb , r , ext , user_ns ))
184+ if (inet_diag_msg_attrs_fill (sk , skb , r , ext , user_ns , net_admin ))
179185 goto errout ;
180186
181187 if (ext & (1 << (INET_DIAG_MEMINFO - 1 ))) {
@@ -274,10 +280,11 @@ static int inet_csk_diag_fill(struct sock *sk,
274280 const struct inet_diag_req_v2 * req ,
275281 struct user_namespace * user_ns ,
276282 u32 portid , u32 seq , u16 nlmsg_flags ,
277- const struct nlmsghdr * unlh )
283+ const struct nlmsghdr * unlh ,
284+ bool net_admin )
278285{
279- return inet_sk_diag_fill (sk , inet_csk (sk ), skb , req ,
280- user_ns , portid , seq , nlmsg_flags , unlh );
286+ return inet_sk_diag_fill (sk , inet_csk (sk ), skb , req , user_ns ,
287+ portid , seq , nlmsg_flags , unlh , net_admin );
281288}
282289
283290static int inet_twsk_diag_fill (struct sock * sk ,
@@ -319,8 +326,9 @@ static int inet_twsk_diag_fill(struct sock *sk,
319326
320327static int inet_req_diag_fill (struct sock * sk , struct sk_buff * skb ,
321328 u32 portid , u32 seq , u16 nlmsg_flags ,
322- const struct nlmsghdr * unlh )
329+ const struct nlmsghdr * unlh , bool net_admin )
323330{
331+ struct request_sock * reqsk = inet_reqsk (sk );
324332 struct inet_diag_msg * r ;
325333 struct nlmsghdr * nlh ;
326334 long tmo ;
@@ -334,7 +342,7 @@ static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
334342 inet_diag_msg_common_fill (r , sk );
335343 r -> idiag_state = TCP_SYN_RECV ;
336344 r -> idiag_timer = 1 ;
337- r -> idiag_retrans = inet_reqsk ( sk ) -> num_retrans ;
345+ r -> idiag_retrans = reqsk -> num_retrans ;
338346
339347 BUILD_BUG_ON (offsetof(struct inet_request_sock , ir_cookie ) !=
340348 offsetof(struct sock , sk_cookie ));
@@ -346,6 +354,10 @@ static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
346354 r -> idiag_uid = 0 ;
347355 r -> idiag_inode = 0 ;
348356
357+ if (net_admin && nla_put_u32 (skb , INET_DIAG_MARK ,
358+ inet_rsk (reqsk )-> ir_mark ))
359+ return - EMSGSIZE ;
360+
349361 nlmsg_end (skb , nlh );
350362 return 0 ;
351363}
@@ -354,18 +366,18 @@ static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
354366 const struct inet_diag_req_v2 * r ,
355367 struct user_namespace * user_ns ,
356368 u32 portid , u32 seq , u16 nlmsg_flags ,
357- const struct nlmsghdr * unlh )
369+ const struct nlmsghdr * unlh , bool net_admin )
358370{
359371 if (sk -> sk_state == TCP_TIME_WAIT )
360372 return inet_twsk_diag_fill (sk , skb , portid , seq ,
361373 nlmsg_flags , unlh );
362374
363375 if (sk -> sk_state == TCP_NEW_SYN_RECV )
364376 return inet_req_diag_fill (sk , skb , portid , seq ,
365- nlmsg_flags , unlh );
377+ nlmsg_flags , unlh , net_admin );
366378
367379 return inet_csk_diag_fill (sk , skb , r , user_ns , portid , seq ,
368- nlmsg_flags , unlh );
380+ nlmsg_flags , unlh , net_admin );
369381}
370382
371383struct sock * inet_diag_find_one_icsk (struct net * net ,
@@ -435,7 +447,8 @@ int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
435447 err = sk_diag_fill (sk , rep , req ,
436448 sk_user_ns (NETLINK_CB (in_skb ).sk ),
437449 NETLINK_CB (in_skb ).portid ,
438- nlh -> nlmsg_seq , 0 , nlh );
450+ nlh -> nlmsg_seq , 0 , nlh ,
451+ netlink_net_capable (in_skb , CAP_NET_ADMIN ));
439452 if (err < 0 ) {
440453 WARN_ON (err == - EMSGSIZE );
441454 nlmsg_free (rep );
@@ -796,15 +809,17 @@ static int inet_csk_diag_dump(struct sock *sk,
796809 struct sk_buff * skb ,
797810 struct netlink_callback * cb ,
798811 const struct inet_diag_req_v2 * r ,
799- const struct nlattr * bc )
812+ const struct nlattr * bc ,
813+ bool net_admin )
800814{
801815 if (!inet_diag_bc_sk (bc , sk ))
802816 return 0 ;
803817
804818 return inet_csk_diag_fill (sk , skb , r ,
805819 sk_user_ns (NETLINK_CB (cb -> skb ).sk ),
806820 NETLINK_CB (cb -> skb ).portid ,
807- cb -> nlh -> nlmsg_seq , NLM_F_MULTI , cb -> nlh );
821+ cb -> nlh -> nlmsg_seq , NLM_F_MULTI , cb -> nlh ,
822+ net_admin );
808823}
809824
810825static void twsk_build_assert (void )
@@ -840,6 +855,7 @@ void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
840855 struct net * net = sock_net (skb -> sk );
841856 int i , num , s_i , s_num ;
842857 u32 idiag_states = r -> idiag_states ;
858+ bool net_admin = netlink_net_capable (cb -> skb , CAP_NET_ADMIN );
843859
844860 if (idiag_states & TCPF_SYN_RECV )
845861 idiag_states |= TCPF_NEW_SYN_RECV ;
@@ -880,7 +896,8 @@ void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
880896 cb -> args [3 ] > 0 )
881897 goto next_listen ;
882898
883- if (inet_csk_diag_dump (sk , skb , cb , r , bc ) < 0 ) {
899+ if (inet_csk_diag_dump (sk , skb , cb , r ,
900+ bc , net_admin ) < 0 ) {
884901 spin_unlock_bh (& ilb -> lock );
885902 goto done ;
886903 }
@@ -948,7 +965,7 @@ void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
948965 sk_user_ns (NETLINK_CB (cb -> skb ).sk ),
949966 NETLINK_CB (cb -> skb ).portid ,
950967 cb -> nlh -> nlmsg_seq , NLM_F_MULTI ,
951- cb -> nlh );
968+ cb -> nlh , net_admin );
952969 if (res < 0 ) {
953970 spin_unlock_bh (lock );
954971 goto done ;
0 commit comments