scm.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __LINUX_NET_SCM_H
  3. #define __LINUX_NET_SCM_H
  4. #include <linux/limits.h>
  5. #include <linux/net.h>
  6. #include <linux/cred.h>
  7. #include <linux/file.h>
  8. #include <linux/security.h>
  9. #include <linux/pid.h>
  10. #include <linux/nsproxy.h>
  11. #include <linux/sched/signal.h>
  12. #include <net/compat.h>
  13. /* Well, we should have at least one descriptor open
  14. * to accept passed FDs 8)
  15. */
  16. #define SCM_MAX_FD 253
  17. struct scm_creds {
  18. u32 pid;
  19. kuid_t uid;
  20. kgid_t gid;
  21. };
  22. #ifdef CONFIG_UNIX
  23. struct unix_edge;
  24. #endif
  25. struct scm_fp_list {
  26. short count;
  27. short count_unix;
  28. short max;
  29. #ifdef CONFIG_UNIX
  30. bool inflight;
  31. bool dead;
  32. struct list_head vertices;
  33. struct unix_edge *edges;
  34. #endif
  35. struct user_struct *user;
  36. struct file *fp[SCM_MAX_FD];
  37. };
  38. struct scm_cookie {
  39. struct pid *pid; /* Skb credentials */
  40. struct scm_fp_list *fp; /* Passed files */
  41. struct scm_creds creds; /* Skb credentials */
  42. #ifdef CONFIG_SECURITY_NETWORK
  43. u32 secid; /* Passed security ID */
  44. #endif
  45. };
  46. void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm);
  47. void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm);
  48. int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm);
  49. void __scm_destroy(struct scm_cookie *scm);
  50. struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl);
  51. #ifdef CONFIG_SECURITY_NETWORK
  52. static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm)
  53. {
  54. security_socket_getpeersec_dgram(sock, NULL, &scm->secid);
  55. }
  56. #else
  57. static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm)
  58. { }
  59. #endif /* CONFIG_SECURITY_NETWORK */
  60. static __inline__ void scm_set_cred(struct scm_cookie *scm,
  61. struct pid *pid, kuid_t uid, kgid_t gid)
  62. {
  63. scm->pid = get_pid(pid);
  64. scm->creds.pid = pid_vnr(pid);
  65. scm->creds.uid = uid;
  66. scm->creds.gid = gid;
  67. }
  68. static __inline__ void scm_destroy_cred(struct scm_cookie *scm)
  69. {
  70. put_pid(scm->pid);
  71. scm->pid = NULL;
  72. }
  73. static __inline__ void scm_destroy(struct scm_cookie *scm)
  74. {
  75. scm_destroy_cred(scm);
  76. if (scm->fp)
  77. __scm_destroy(scm);
  78. }
  79. static __inline__ int scm_send(struct socket *sock, struct msghdr *msg,
  80. struct scm_cookie *scm, bool forcecreds)
  81. {
  82. memset(scm, 0, sizeof(*scm));
  83. scm->creds.uid = INVALID_UID;
  84. scm->creds.gid = INVALID_GID;
  85. if (forcecreds)
  86. scm_set_cred(scm, task_tgid(current), current_uid(), current_gid());
  87. unix_get_peersec_dgram(sock, scm);
  88. if (msg->msg_controllen <= 0)
  89. return 0;
  90. return __scm_send(sock, msg, scm);
  91. }
  92. #ifdef CONFIG_SECURITY_NETWORK
  93. static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm)
  94. {
  95. char *secdata;
  96. u32 seclen;
  97. int err;
  98. if (test_bit(SOCK_PASSSEC, &sock->flags)) {
  99. err = security_secid_to_secctx(scm->secid, &secdata, &seclen);
  100. if (!err) {
  101. put_cmsg(msg, SOL_SOCKET, SCM_SECURITY, seclen, secdata);
  102. security_release_secctx(secdata, seclen);
  103. }
  104. }
  105. }
  106. static inline bool scm_has_secdata(struct socket *sock)
  107. {
  108. return test_bit(SOCK_PASSSEC, &sock->flags);
  109. }
  110. #else
  111. static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm)
  112. { }
  113. static inline bool scm_has_secdata(struct socket *sock)
  114. {
  115. return false;
  116. }
  117. #endif /* CONFIG_SECURITY_NETWORK */
  118. static __inline__ void scm_pidfd_recv(struct msghdr *msg, struct scm_cookie *scm)
  119. {
  120. struct file *pidfd_file = NULL;
  121. int len, pidfd;
  122. /* put_cmsg() doesn't return an error if CMSG is truncated,
  123. * that's why we need to opencode these checks here.
  124. */
  125. if (msg->msg_flags & MSG_CMSG_COMPAT)
  126. len = sizeof(struct compat_cmsghdr) + sizeof(int);
  127. else
  128. len = sizeof(struct cmsghdr) + sizeof(int);
  129. if (msg->msg_controllen < len) {
  130. msg->msg_flags |= MSG_CTRUNC;
  131. return;
  132. }
  133. if (!scm->pid)
  134. return;
  135. pidfd = pidfd_prepare(scm->pid, 0, &pidfd_file);
  136. if (put_cmsg(msg, SOL_SOCKET, SCM_PIDFD, sizeof(int), &pidfd)) {
  137. if (pidfd_file) {
  138. put_unused_fd(pidfd);
  139. fput(pidfd_file);
  140. }
  141. return;
  142. }
  143. if (pidfd_file)
  144. fd_install(pidfd, pidfd_file);
  145. }
  146. static inline bool __scm_recv_common(struct socket *sock, struct msghdr *msg,
  147. struct scm_cookie *scm, int flags)
  148. {
  149. if (!msg->msg_control) {
  150. if (test_bit(SOCK_PASSCRED, &sock->flags) ||
  151. test_bit(SOCK_PASSPIDFD, &sock->flags) ||
  152. scm->fp || scm_has_secdata(sock))
  153. msg->msg_flags |= MSG_CTRUNC;
  154. scm_destroy(scm);
  155. return false;
  156. }
  157. if (test_bit(SOCK_PASSCRED, &sock->flags)) {
  158. struct user_namespace *current_ns = current_user_ns();
  159. struct ucred ucreds = {
  160. .pid = scm->creds.pid,
  161. .uid = from_kuid_munged(current_ns, scm->creds.uid),
  162. .gid = from_kgid_munged(current_ns, scm->creds.gid),
  163. };
  164. put_cmsg(msg, SOL_SOCKET, SCM_CREDENTIALS, sizeof(ucreds), &ucreds);
  165. }
  166. scm_passec(sock, msg, scm);
  167. if (scm->fp)
  168. scm_detach_fds(msg, scm);
  169. return true;
  170. }
  171. static inline void scm_recv(struct socket *sock, struct msghdr *msg,
  172. struct scm_cookie *scm, int flags)
  173. {
  174. if (!__scm_recv_common(sock, msg, scm, flags))
  175. return;
  176. scm_destroy_cred(scm);
  177. }
  178. static inline void scm_recv_unix(struct socket *sock, struct msghdr *msg,
  179. struct scm_cookie *scm, int flags)
  180. {
  181. if (!__scm_recv_common(sock, msg, scm, flags))
  182. return;
  183. if (test_bit(SOCK_PASSPIDFD, &sock->flags))
  184. scm_pidfd_recv(msg, scm);
  185. scm_destroy_cred(scm);
  186. }
  187. static inline int scm_recv_one_fd(struct file *f, int __user *ufd,
  188. unsigned int flags)
  189. {
  190. if (!ufd)
  191. return -EFAULT;
  192. return receive_fd(f, ufd, flags);
  193. }
  194. #endif /* __LINUX_NET_SCM_H */