2017-11-01 15:07:57 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2005-04-16 15:20:36 -07:00
|
|
|
#ifndef __LINUX_NET_SCM_H
|
|
|
|
|
#define __LINUX_NET_SCM_H
|
|
|
|
|
|
|
|
|
|
#include <linux/limits.h>
|
|
|
|
|
#include <linux/net.h>
|
2017-02-02 17:54:15 +01:00
|
|
|
#include <linux/cred.h>
|
2023-11-30 13:49:10 +01:00
|
|
|
#include <linux/file.h>
|
2006-08-02 14:12:06 -07:00
|
|
|
#include <linux/security.h>
|
2007-10-18 23:40:14 -07:00
|
|
|
#include <linux/pid.h>
|
|
|
|
|
#include <linux/nsproxy.h>
|
2017-09-26 12:45:33 -05:00
|
|
|
#include <linux/sched/signal.h>
|
2023-09-01 16:46:04 -07:00
|
|
|
#include <net/compat.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
|
/* Well, we should have at least one descriptor open
|
|
|
|
|
* to accept passed FDs 8)
|
|
|
|
|
*/
|
2010-11-23 14:09:15 +00:00
|
|
|
#define SCM_MAX_FD 253
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2012-09-06 18:20:01 +00:00
|
|
|
struct scm_creds {
|
|
|
|
|
u32 pid;
|
|
|
|
|
kuid_t uid;
|
|
|
|
|
kgid_t gid;
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-25 13:24:12 -07:00
|
|
|
#ifdef CONFIG_UNIX
|
|
|
|
|
struct unix_edge;
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-11-03 03:26:03 +00:00
|
|
|
struct scm_fp_list {
|
2010-11-23 14:09:15 +00:00
|
|
|
short count;
|
2024-01-23 09:08:56 -08:00
|
|
|
short count_unix;
|
2010-11-23 14:09:15 +00:00
|
|
|
short max;
|
2024-03-25 13:24:11 -07:00
|
|
|
#ifdef CONFIG_UNIX
|
2024-03-25 13:24:13 -07:00
|
|
|
bool inflight;
|
2024-05-08 10:11:50 -07:00
|
|
|
bool dead;
|
2024-03-25 13:24:11 -07:00
|
|
|
struct list_head vertices;
|
2024-03-25 13:24:12 -07:00
|
|
|
struct unix_edge *edges;
|
2024-03-25 13:24:11 -07:00
|
|
|
#endif
|
2016-02-03 02:11:03 +01:00
|
|
|
struct user_struct *user;
|
2008-11-06 00:37:40 -08:00
|
|
|
struct file *fp[SCM_MAX_FD];
|
2005-04-16 15:20:36 -07:00
|
|
|
};
|
|
|
|
|
|
2009-11-03 03:26:03 +00:00
|
|
|
struct scm_cookie {
|
2010-06-13 03:32:34 +00:00
|
|
|
struct pid *pid; /* Skb credentials */
|
2005-04-16 15:20:36 -07:00
|
|
|
struct scm_fp_list *fp; /* Passed files */
|
2012-09-06 18:20:01 +00:00
|
|
|
struct scm_creds creds; /* Skb credentials */
|
2006-06-29 12:27:47 -07:00
|
|
|
#ifdef CONFIG_SECURITY_NETWORK
|
2006-08-02 14:12:06 -07:00
|
|
|
u32 secid; /* Passed security ID */
|
2006-06-29 12:27:47 -07:00
|
|
|
#endif
|
2005-04-16 15:20:36 -07:00
|
|
|
};
|
|
|
|
|
|
2013-09-22 10:32:24 -07:00
|
|
|
void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm);
|
|
|
|
|
void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm);
|
|
|
|
|
int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm);
|
|
|
|
|
void __scm_destroy(struct scm_cookie *scm);
|
|
|
|
|
struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2006-08-02 14:12:06 -07:00
|
|
|
#ifdef CONFIG_SECURITY_NETWORK
|
|
|
|
|
static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm)
|
|
|
|
|
{
|
|
|
|
|
security_socket_getpeersec_dgram(sock, NULL, &scm->secid);
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm)
|
|
|
|
|
{ }
|
|
|
|
|
#endif /* CONFIG_SECURITY_NETWORK */
|
|
|
|
|
|
2010-06-13 03:32:34 +00:00
|
|
|
static __inline__ void scm_set_cred(struct scm_cookie *scm,
|
2013-04-03 17:28:16 +00:00
|
|
|
struct pid *pid, kuid_t uid, kgid_t gid)
|
2010-06-13 03:32:34 +00:00
|
|
|
{
|
2025-07-04 00:23:08 +02:00
|
|
|
scm->pid = get_pid(pid);
|
2012-09-06 18:20:01 +00:00
|
|
|
scm->creds.pid = pid_vnr(pid);
|
2013-04-03 17:28:16 +00:00
|
|
|
scm->creds.uid = uid;
|
|
|
|
|
scm->creds.gid = gid;
|
2010-06-13 03:32:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static __inline__ void scm_destroy_cred(struct scm_cookie *scm)
|
|
|
|
|
{
|
|
|
|
|
put_pid(scm->pid);
|
2025-07-04 00:23:08 +02:00
|
|
|
scm->pid = NULL;
|
2010-06-13 03:32:34 +00:00
|
|
|
}
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
static __inline__ void scm_destroy(struct scm_cookie *scm)
|
|
|
|
|
{
|
2010-06-13 03:32:34 +00:00
|
|
|
scm_destroy_cred(scm);
|
2012-09-24 15:52:33 -04:00
|
|
|
if (scm->fp)
|
2005-04-16 15:20:36 -07:00
|
|
|
__scm_destroy(scm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static __inline__ int scm_send(struct socket *sock, struct msghdr *msg,
|
2012-08-21 06:21:17 +00:00
|
|
|
struct scm_cookie *scm, bool forcecreds)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2011-09-19 05:52:27 +00:00
|
|
|
memset(scm, 0, sizeof(*scm));
|
2013-04-03 17:28:16 +00:00
|
|
|
scm->creds.uid = INVALID_UID;
|
|
|
|
|
scm->creds.gid = INVALID_GID;
|
2012-08-21 06:21:17 +00:00
|
|
|
if (forcecreds)
|
2013-04-22 20:32:51 -04:00
|
|
|
scm_set_cred(scm, task_tgid(current), current_uid(), current_gid());
|
2006-08-02 14:12:06 -07:00
|
|
|
unix_get_peersec_dgram(sock, scm);
|
2005-04-16 15:20:36 -07:00
|
|
|
if (msg->msg_controllen <= 0)
|
|
|
|
|
return 0;
|
|
|
|
|
return __scm_send(sock, msg, scm);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-19 13:57:54 -07:00
|
|
|
void scm_recv(struct socket *sock, struct msghdr *msg,
|
|
|
|
|
struct scm_cookie *scm, int flags);
|
|
|
|
|
void scm_recv_unix(struct socket *sock, struct msghdr *msg,
|
|
|
|
|
struct scm_cookie *scm, int flags);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2023-11-30 13:49:10 +01:00
|
|
|
static inline int scm_recv_one_fd(struct file *f, int __user *ufd,
|
|
|
|
|
unsigned int flags)
|
|
|
|
|
{
|
|
|
|
|
if (!ufd)
|
|
|
|
|
return -EFAULT;
|
2023-11-30 13:49:11 +01:00
|
|
|
return receive_fd(f, ufd, flags);
|
2023-11-30 13:49:10 +01:00
|
|
|
}
|
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
#endif /* __LINUX_NET_SCM_H */
|
|
|
|
|
|