xref: /openbmc/linux/include/net/scm.h (revision a02d83f9947d8f71904eda4de046630c3eb6802c)
1b2441318SGreg 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>
117a36094dSEric W. Biederman #include <linux/sched/signal.h>
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds /* Well, we should have at least one descriptor open
141da177e4SLinus Torvalds  * to accept passed FDs 8)
151da177e4SLinus Torvalds  */
16bba14de9SEric Dumazet #define SCM_MAX_FD	253
171da177e4SLinus Torvalds 
18dbe9a417SEric W. Biederman struct scm_creds {
19dbe9a417SEric W. Biederman 	u32	pid;
20dbe9a417SEric W. Biederman 	kuid_t	uid;
21dbe9a417SEric W. Biederman 	kgid_t	gid;
22dbe9a417SEric W. Biederman };
23dbe9a417SEric W. Biederman 
24fd2c3ef7SEric Dumazet struct scm_fp_list {
25bba14de9SEric Dumazet 	short			count;
26bba14de9SEric Dumazet 	short			max;
27415e3d3eSHannes Frederic Sowa 	struct user_struct	*user;
281da177e4SLinus Torvalds 	struct file		*fp[SCM_MAX_FD];
291da177e4SLinus Torvalds };
301da177e4SLinus Torvalds 
31fd2c3ef7SEric Dumazet struct scm_cookie {
32257b5358SEric W. Biederman 	struct pid		*pid;		/* Skb credentials */
331da177e4SLinus Torvalds 	struct scm_fp_list	*fp;		/* Passed files		*/
34dbe9a417SEric W. Biederman 	struct scm_creds	creds;		/* Skb credentials	*/
35877ce7c1SCatherine Zhang #ifdef CONFIG_SECURITY_NETWORK
36dc49c1f9SCatherine Zhang 	u32			secid;		/* Passed security ID 	*/
37877ce7c1SCatherine Zhang #endif
381da177e4SLinus Torvalds };
391da177e4SLinus Torvalds 
408153ff5cSJoe Perches void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm);
418153ff5cSJoe Perches void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm);
428153ff5cSJoe Perches int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm);
438153ff5cSJoe Perches void __scm_destroy(struct scm_cookie *scm);
448153ff5cSJoe Perches struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl);
451da177e4SLinus Torvalds 
46dc49c1f9SCatherine Zhang #ifdef CONFIG_SECURITY_NETWORK
47dc49c1f9SCatherine Zhang static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm)
48dc49c1f9SCatherine Zhang {
49dc49c1f9SCatherine Zhang 	security_socket_getpeersec_dgram(sock, NULL, &scm->secid);
50dc49c1f9SCatherine Zhang }
51dc49c1f9SCatherine Zhang #else
52dc49c1f9SCatherine Zhang static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm)
53dc49c1f9SCatherine Zhang { }
54dc49c1f9SCatherine Zhang #endif /* CONFIG_SECURITY_NETWORK */
55dc49c1f9SCatherine Zhang 
56257b5358SEric W. Biederman static __inline__ void scm_set_cred(struct scm_cookie *scm,
576b0ee8c0SEric W. Biederman 				    struct pid *pid, kuid_t uid, kgid_t gid)
58257b5358SEric W. Biederman {
59257b5358SEric W. Biederman 	scm->pid  = get_pid(pid);
60dbe9a417SEric W. Biederman 	scm->creds.pid = pid_vnr(pid);
616b0ee8c0SEric W. Biederman 	scm->creds.uid = uid;
626b0ee8c0SEric W. Biederman 	scm->creds.gid = gid;
63257b5358SEric W. Biederman }
64257b5358SEric W. Biederman 
65257b5358SEric W. Biederman static __inline__ void scm_destroy_cred(struct scm_cookie *scm)
66257b5358SEric W. Biederman {
67257b5358SEric W. Biederman 	put_pid(scm->pid);
68257b5358SEric W. Biederman 	scm->pid  = NULL;
69257b5358SEric W. Biederman }
70257b5358SEric W. Biederman 
711da177e4SLinus Torvalds static __inline__ void scm_destroy(struct scm_cookie *scm)
721da177e4SLinus Torvalds {
73257b5358SEric W. Biederman 	scm_destroy_cred(scm);
742a6c8c79SDavid S. Miller 	if (scm->fp)
751da177e4SLinus Torvalds 		__scm_destroy(scm);
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds static __inline__ int scm_send(struct socket *sock, struct msghdr *msg,
79e0e3cea4SEric Dumazet 			       struct scm_cookie *scm, bool forcecreds)
801da177e4SLinus Torvalds {
8116e57262SEric Dumazet 	memset(scm, 0, sizeof(*scm));
826b0ee8c0SEric W. Biederman 	scm->creds.uid = INVALID_UID;
836b0ee8c0SEric W. Biederman 	scm->creds.gid = INVALID_GID;
84e0e3cea4SEric Dumazet 	if (forcecreds)
856e0895c2SDavid S. Miller 		scm_set_cred(scm, task_tgid(current), current_uid(), current_gid());
86dc49c1f9SCatherine Zhang 	unix_get_peersec_dgram(sock, scm);
871da177e4SLinus Torvalds 	if (msg->msg_controllen <= 0)
881da177e4SLinus Torvalds 		return 0;
891da177e4SLinus Torvalds 	return __scm_send(sock, msg, scm);
901da177e4SLinus Torvalds }
911da177e4SLinus Torvalds 
92877ce7c1SCatherine Zhang #ifdef CONFIG_SECURITY_NETWORK
93877ce7c1SCatherine Zhang static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm)
94877ce7c1SCatherine Zhang {
95dc49c1f9SCatherine Zhang 	char *secdata;
96dc49c1f9SCatherine Zhang 	u32 seclen;
97dc49c1f9SCatherine Zhang 	int err;
98dc49c1f9SCatherine Zhang 
99dc49c1f9SCatherine Zhang 	if (test_bit(SOCK_PASSSEC, &sock->flags)) {
100dc49c1f9SCatherine Zhang 		err = security_secid_to_secctx(scm->secid, &secdata, &seclen);
101dc49c1f9SCatherine Zhang 
102dc49c1f9SCatherine Zhang 		if (!err) {
103dc49c1f9SCatherine Zhang 			put_cmsg(msg, SOL_SOCKET, SCM_SECURITY, seclen, secdata);
104dc49c1f9SCatherine Zhang 			security_release_secctx(secdata, seclen);
105dc49c1f9SCatherine Zhang 		}
106dc49c1f9SCatherine Zhang 	}
107877ce7c1SCatherine Zhang }
108*a02d83f9SAlexander Mikhalitsyn 
109*a02d83f9SAlexander Mikhalitsyn static inline bool scm_has_secdata(struct socket *sock)
110*a02d83f9SAlexander Mikhalitsyn {
111*a02d83f9SAlexander Mikhalitsyn 	return test_bit(SOCK_PASSSEC, &sock->flags);
112*a02d83f9SAlexander Mikhalitsyn }
113877ce7c1SCatherine Zhang #else
114877ce7c1SCatherine Zhang static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm)
115877ce7c1SCatherine Zhang { }
116*a02d83f9SAlexander Mikhalitsyn 
117*a02d83f9SAlexander Mikhalitsyn static inline bool scm_has_secdata(struct socket *sock)
118*a02d83f9SAlexander Mikhalitsyn {
119*a02d83f9SAlexander Mikhalitsyn 	return false;
120*a02d83f9SAlexander Mikhalitsyn }
121877ce7c1SCatherine Zhang #endif /* CONFIG_SECURITY_NETWORK */
122877ce7c1SCatherine Zhang 
1231da177e4SLinus Torvalds static __inline__ void scm_recv(struct socket *sock, struct msghdr *msg,
1241da177e4SLinus Torvalds 				struct scm_cookie *scm, int flags)
1251da177e4SLinus Torvalds {
126fd2c3ef7SEric Dumazet 	if (!msg->msg_control) {
127*a02d83f9SAlexander Mikhalitsyn 		if (test_bit(SOCK_PASSCRED, &sock->flags) || scm->fp ||
128*a02d83f9SAlexander Mikhalitsyn 		    scm_has_secdata(sock))
1291da177e4SLinus Torvalds 			msg->msg_flags |= MSG_CTRUNC;
130f78a5fdaSDavid S. Miller 		scm_destroy(scm);
1311da177e4SLinus Torvalds 		return;
1321da177e4SLinus Torvalds 	}
1331da177e4SLinus Torvalds 
134dbe9a417SEric W. Biederman 	if (test_bit(SOCK_PASSCRED, &sock->flags)) {
135dbe9a417SEric W. Biederman 		struct user_namespace *current_ns = current_user_ns();
136dbe9a417SEric W. Biederman 		struct ucred ucreds = {
137dbe9a417SEric W. Biederman 			.pid = scm->creds.pid,
138dbe9a417SEric W. Biederman 			.uid = from_kuid_munged(current_ns, scm->creds.uid),
139dbe9a417SEric W. Biederman 			.gid = from_kgid_munged(current_ns, scm->creds.gid),
140dbe9a417SEric W. Biederman 		};
141dbe9a417SEric W. Biederman 		put_cmsg(msg, SOL_SOCKET, SCM_CREDENTIALS, sizeof(ucreds), &ucreds);
142dbe9a417SEric W. Biederman 	}
1431da177e4SLinus Torvalds 
144f78a5fdaSDavid S. Miller 	scm_destroy_cred(scm);
145f78a5fdaSDavid S. Miller 
146877ce7c1SCatherine Zhang 	scm_passec(sock, msg, scm);
147877ce7c1SCatherine Zhang 
1481da177e4SLinus Torvalds 	if (!scm->fp)
1491da177e4SLinus Torvalds 		return;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	scm_detach_fds(msg, scm);
1521da177e4SLinus Torvalds }
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds #endif /* __LINUX_NET_SCM_H */
1561da177e4SLinus Torvalds 
157