1*b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 21da177e4SLinus Torvalds #ifndef __LINUX_NET_SCM_H 31da177e4SLinus Torvalds #define __LINUX_NET_SCM_H 41da177e4SLinus Torvalds 51da177e4SLinus Torvalds #include <linux/limits.h> 61da177e4SLinus Torvalds #include <linux/net.h> 75b825c3aSIngo Molnar #include <linux/cred.h> 8dc49c1f9SCatherine Zhang #include <linux/security.h> 9b488893aSPavel Emelyanov #include <linux/pid.h> 10b488893aSPavel Emelyanov #include <linux/nsproxy.h> 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds /* Well, we should have at least one descriptor open 131da177e4SLinus Torvalds * to accept passed FDs 8) 141da177e4SLinus Torvalds */ 15bba14de9SEric Dumazet #define SCM_MAX_FD 253 161da177e4SLinus Torvalds 17dbe9a417SEric W. Biederman struct scm_creds { 18dbe9a417SEric W. Biederman u32 pid; 19dbe9a417SEric W. Biederman kuid_t uid; 20dbe9a417SEric W. Biederman kgid_t gid; 21dbe9a417SEric W. Biederman }; 22dbe9a417SEric W. Biederman 23fd2c3ef7SEric Dumazet struct scm_fp_list { 24bba14de9SEric Dumazet short count; 25bba14de9SEric Dumazet short max; 26415e3d3eSHannes Frederic Sowa struct user_struct *user; 271da177e4SLinus Torvalds struct file *fp[SCM_MAX_FD]; 281da177e4SLinus Torvalds }; 291da177e4SLinus Torvalds 30fd2c3ef7SEric Dumazet struct scm_cookie { 31257b5358SEric W. Biederman struct pid *pid; /* Skb credentials */ 321da177e4SLinus Torvalds struct scm_fp_list *fp; /* Passed files */ 33dbe9a417SEric W. Biederman struct scm_creds creds; /* Skb credentials */ 34877ce7c1SCatherine Zhang #ifdef CONFIG_SECURITY_NETWORK 35dc49c1f9SCatherine Zhang u32 secid; /* Passed security ID */ 36877ce7c1SCatherine Zhang #endif 371da177e4SLinus Torvalds }; 381da177e4SLinus Torvalds 398153ff5cSJoe Perches void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm); 408153ff5cSJoe Perches void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm); 418153ff5cSJoe Perches int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm); 428153ff5cSJoe Perches void __scm_destroy(struct scm_cookie *scm); 438153ff5cSJoe Perches struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl); 441da177e4SLinus Torvalds 45dc49c1f9SCatherine Zhang #ifdef CONFIG_SECURITY_NETWORK 46dc49c1f9SCatherine Zhang static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm) 47dc49c1f9SCatherine Zhang { 48dc49c1f9SCatherine Zhang security_socket_getpeersec_dgram(sock, NULL, &scm->secid); 49dc49c1f9SCatherine Zhang } 50dc49c1f9SCatherine Zhang #else 51dc49c1f9SCatherine Zhang static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm) 52dc49c1f9SCatherine Zhang { } 53dc49c1f9SCatherine Zhang #endif /* CONFIG_SECURITY_NETWORK */ 54dc49c1f9SCatherine Zhang 55257b5358SEric W. Biederman static __inline__ void scm_set_cred(struct scm_cookie *scm, 566b0ee8c0SEric W. Biederman struct pid *pid, kuid_t uid, kgid_t gid) 57257b5358SEric W. Biederman { 58257b5358SEric W. Biederman scm->pid = get_pid(pid); 59dbe9a417SEric W. Biederman scm->creds.pid = pid_vnr(pid); 606b0ee8c0SEric W. Biederman scm->creds.uid = uid; 616b0ee8c0SEric W. Biederman scm->creds.gid = gid; 62257b5358SEric W. Biederman } 63257b5358SEric W. Biederman 64257b5358SEric W. Biederman static __inline__ void scm_destroy_cred(struct scm_cookie *scm) 65257b5358SEric W. Biederman { 66257b5358SEric W. Biederman put_pid(scm->pid); 67257b5358SEric W. Biederman scm->pid = NULL; 68257b5358SEric W. Biederman } 69257b5358SEric W. Biederman 701da177e4SLinus Torvalds static __inline__ void scm_destroy(struct scm_cookie *scm) 711da177e4SLinus Torvalds { 72257b5358SEric W. Biederman scm_destroy_cred(scm); 732a6c8c79SDavid S. Miller if (scm->fp) 741da177e4SLinus Torvalds __scm_destroy(scm); 751da177e4SLinus Torvalds } 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds static __inline__ int scm_send(struct socket *sock, struct msghdr *msg, 78e0e3cea4SEric Dumazet struct scm_cookie *scm, bool forcecreds) 791da177e4SLinus Torvalds { 8016e57262SEric Dumazet memset(scm, 0, sizeof(*scm)); 816b0ee8c0SEric W. Biederman scm->creds.uid = INVALID_UID; 826b0ee8c0SEric W. Biederman scm->creds.gid = INVALID_GID; 83e0e3cea4SEric Dumazet if (forcecreds) 846e0895c2SDavid S. Miller scm_set_cred(scm, task_tgid(current), current_uid(), current_gid()); 85dc49c1f9SCatherine Zhang unix_get_peersec_dgram(sock, scm); 861da177e4SLinus Torvalds if (msg->msg_controllen <= 0) 871da177e4SLinus Torvalds return 0; 881da177e4SLinus Torvalds return __scm_send(sock, msg, scm); 891da177e4SLinus Torvalds } 901da177e4SLinus Torvalds 91877ce7c1SCatherine Zhang #ifdef CONFIG_SECURITY_NETWORK 92877ce7c1SCatherine Zhang static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm) 93877ce7c1SCatherine Zhang { 94dc49c1f9SCatherine Zhang char *secdata; 95dc49c1f9SCatherine Zhang u32 seclen; 96dc49c1f9SCatherine Zhang int err; 97dc49c1f9SCatherine Zhang 98dc49c1f9SCatherine Zhang if (test_bit(SOCK_PASSSEC, &sock->flags)) { 99dc49c1f9SCatherine Zhang err = security_secid_to_secctx(scm->secid, &secdata, &seclen); 100dc49c1f9SCatherine Zhang 101dc49c1f9SCatherine Zhang if (!err) { 102dc49c1f9SCatherine Zhang put_cmsg(msg, SOL_SOCKET, SCM_SECURITY, seclen, secdata); 103dc49c1f9SCatherine Zhang security_release_secctx(secdata, seclen); 104dc49c1f9SCatherine Zhang } 105dc49c1f9SCatherine Zhang } 106877ce7c1SCatherine Zhang } 107877ce7c1SCatherine Zhang #else 108877ce7c1SCatherine Zhang static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm) 109877ce7c1SCatherine Zhang { } 110877ce7c1SCatherine Zhang #endif /* CONFIG_SECURITY_NETWORK */ 111877ce7c1SCatherine Zhang 1121da177e4SLinus Torvalds static __inline__ void scm_recv(struct socket *sock, struct msghdr *msg, 1131da177e4SLinus Torvalds struct scm_cookie *scm, int flags) 1141da177e4SLinus Torvalds { 115fd2c3ef7SEric Dumazet if (!msg->msg_control) { 1161da177e4SLinus Torvalds if (test_bit(SOCK_PASSCRED, &sock->flags) || scm->fp) 1171da177e4SLinus Torvalds msg->msg_flags |= MSG_CTRUNC; 118f78a5fdaSDavid S. Miller scm_destroy(scm); 1191da177e4SLinus Torvalds return; 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds 122dbe9a417SEric W. Biederman if (test_bit(SOCK_PASSCRED, &sock->flags)) { 123dbe9a417SEric W. Biederman struct user_namespace *current_ns = current_user_ns(); 124dbe9a417SEric W. Biederman struct ucred ucreds = { 125dbe9a417SEric W. Biederman .pid = scm->creds.pid, 126dbe9a417SEric W. Biederman .uid = from_kuid_munged(current_ns, scm->creds.uid), 127dbe9a417SEric W. Biederman .gid = from_kgid_munged(current_ns, scm->creds.gid), 128dbe9a417SEric W. Biederman }; 129dbe9a417SEric W. Biederman put_cmsg(msg, SOL_SOCKET, SCM_CREDENTIALS, sizeof(ucreds), &ucreds); 130dbe9a417SEric W. Biederman } 1311da177e4SLinus Torvalds 132f78a5fdaSDavid S. Miller scm_destroy_cred(scm); 133f78a5fdaSDavid S. Miller 134877ce7c1SCatherine Zhang scm_passec(sock, msg, scm); 135877ce7c1SCatherine Zhang 1361da177e4SLinus Torvalds if (!scm->fp) 1371da177e4SLinus Torvalds return; 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds scm_detach_fds(msg, scm); 1401da177e4SLinus Torvalds } 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds #endif /* __LINUX_NET_SCM_H */ 1441da177e4SLinus Torvalds 145