@@ -1929,7 +1929,7 @@ static void unix_destruct_scm(struct sk_buff *skb)
1929
1929
struct scm_cookie scm ;
1930
1930
1931
1931
memset (& scm , 0 , sizeof (scm ));
1932
- scm .pid = UNIXCB (skb ).pid ;
1932
+ scm .pid = UNIXCB (skb ).pid ;
1933
1933
if (UNIXCB (skb ).fp )
1934
1934
unix_detach_fds (& scm , skb );
1935
1935
@@ -1955,21 +1955,45 @@ static int unix_scm_to_skb(struct scm_cookie *scm, struct sk_buff *skb, bool sen
1955
1955
return err ;
1956
1956
}
1957
1957
1958
- /*
1958
+ static void unix_skb_to_scm (struct sk_buff * skb , struct scm_cookie * scm )
1959
+ {
1960
+ scm_set_cred (scm , UNIXCB (skb ).pid , UNIXCB (skb ).uid , UNIXCB (skb ).gid );
1961
+ unix_set_secdata (scm , skb );
1962
+ }
1963
+
1964
+ /**
1965
+ * unix_maybe_add_creds() - Adds current task uid/gid and struct pid to skb if needed.
1966
+ * @skb: skb to attach creds to.
1967
+ * @sk: Sender sock.
1968
+ * @other: Receiver sock.
1969
+ *
1959
1970
* Some apps rely on write() giving SCM_CREDENTIALS
1960
1971
* We include credentials if source or destination socket
1961
1972
* asserted SOCK_PASSCRED.
1973
+ *
1974
+ * Context: May sleep.
1975
+ * Return: On success zero, on error a negative error code is returned.
1962
1976
*/
1963
- static void unix_maybe_add_creds (struct sk_buff * skb , const struct sock * sk ,
1964
- const struct sock * other )
1977
+ static int unix_maybe_add_creds (struct sk_buff * skb , const struct sock * sk ,
1978
+ const struct sock * other )
1965
1979
{
1966
1980
if (UNIXCB (skb ).pid )
1967
- return ;
1981
+ return 0 ;
1968
1982
1969
1983
if (unix_may_passcred (sk ) || unix_may_passcred (other )) {
1970
- UNIXCB (skb ).pid = get_pid (task_tgid (current ));
1984
+ struct pid * pid ;
1985
+ int err ;
1986
+
1987
+ pid = task_tgid (current );
1988
+ err = pidfs_register_pid (pid );
1989
+ if (unlikely (err ))
1990
+ return err ;
1991
+
1992
+ UNIXCB (skb ).pid = get_pid (pid );
1971
1993
current_uid_gid (& UNIXCB (skb ).uid , & UNIXCB (skb ).gid );
1972
1994
}
1995
+
1996
+ return 0 ;
1973
1997
}
1974
1998
1975
1999
static bool unix_skb_scm_eq (struct sk_buff * skb ,
@@ -2104,6 +2128,10 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
2104
2128
goto out_sock_put ;
2105
2129
}
2106
2130
2131
+ err = unix_maybe_add_creds (skb , sk , other );
2132
+ if (err )
2133
+ goto out_sock_put ;
2134
+
2107
2135
restart :
2108
2136
sk_locked = 0 ;
2109
2137
unix_state_lock (other );
@@ -2212,7 +2240,6 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
2212
2240
if (sock_flag (other , SOCK_RCVTSTAMP ))
2213
2241
__net_timestamp (skb );
2214
2242
2215
- unix_maybe_add_creds (skb , sk , other );
2216
2243
scm_stat_add (other , skb );
2217
2244
skb_queue_tail (& other -> sk_receive_queue , skb );
2218
2245
unix_state_unlock (other );
@@ -2256,6 +2283,10 @@ static int queue_oob(struct sock *sk, struct msghdr *msg, struct sock *other,
2256
2283
if (err < 0 )
2257
2284
goto out ;
2258
2285
2286
+ err = unix_maybe_add_creds (skb , sk , other );
2287
+ if (err )
2288
+ goto out ;
2289
+
2259
2290
skb_put (skb , 1 );
2260
2291
err = skb_copy_datagram_from_iter (skb , 0 , & msg -> msg_iter , 1 );
2261
2292
@@ -2275,7 +2306,6 @@ static int queue_oob(struct sock *sk, struct msghdr *msg, struct sock *other,
2275
2306
goto out_unlock ;
2276
2307
}
2277
2308
2278
- unix_maybe_add_creds (skb , sk , other );
2279
2309
scm_stat_add (other , skb );
2280
2310
2281
2311
spin_lock (& other -> sk_receive_queue .lock );
@@ -2369,6 +2399,10 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,
2369
2399
2370
2400
fds_sent = true;
2371
2401
2402
+ err = unix_maybe_add_creds (skb , sk , other );
2403
+ if (err )
2404
+ goto out_free ;
2405
+
2372
2406
if (unlikely (msg -> msg_flags & MSG_SPLICE_PAGES )) {
2373
2407
skb -> ip_summed = CHECKSUM_UNNECESSARY ;
2374
2408
err = skb_splice_from_iter (skb , & msg -> msg_iter , size ,
@@ -2399,7 +2433,6 @@ static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,
2399
2433
goto out_free ;
2400
2434
}
2401
2435
2402
- unix_maybe_add_creds (skb , sk , other );
2403
2436
scm_stat_add (other , skb );
2404
2437
skb_queue_tail (& other -> sk_receive_queue , skb );
2405
2438
unix_state_unlock (other );
@@ -2547,8 +2580,7 @@ int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size,
2547
2580
2548
2581
memset (& scm , 0 , sizeof (scm ));
2549
2582
2550
- scm_set_cred (& scm , UNIXCB (skb ).pid , UNIXCB (skb ).uid , UNIXCB (skb ).gid );
2551
- unix_set_secdata (& scm , skb );
2583
+ unix_skb_to_scm (skb , & scm );
2552
2584
2553
2585
if (!(flags & MSG_PEEK )) {
2554
2586
if (UNIXCB (skb ).fp )
@@ -2933,8 +2965,7 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
2933
2965
break ;
2934
2966
} else if (unix_may_passcred (sk )) {
2935
2967
/* Copy credentials */
2936
- scm_set_cred (& scm , UNIXCB (skb ).pid , UNIXCB (skb ).uid , UNIXCB (skb ).gid );
2937
- unix_set_secdata (& scm , skb );
2968
+ unix_skb_to_scm (skb , & scm );
2938
2969
check_creds = true;
2939
2970
}
2940
2971
0 commit comments