scm.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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/security.h>
  8. #include <linux/pid.h>
  9. #include <linux/nsproxy.h>
  10. #include <linux/sched/signal.h>
  11. /* Well, we should have at least one descriptor open
  12. * to accept passed FDs 8)
  13. */
  14. #define SCM_MAX_FD 253
  15. struct scm_creds {
  16. u32 pid;
  17. kuid_t uid;
  18. kgid_t gid;
  19. };
  20. struct scm_fp_list {
  21. short count;
  22. short max;
  23. struct user_struct *user;
  24. struct file *fp[SCM_MAX_FD];
  25. };
  26. struct scm_cookie {
  27. struct pid *pid; /* Skb credentials */
  28. struct scm_fp_list *fp; /* Passed files */
  29. struct scm_creds creds; /* Skb credentials */
  30. #ifdef CONFIG_SECURITY_NETWORK
  31. u32 secid; /* Passed security ID */
  32. #endif
  33. };
  34. void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm);
  35. void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm);
  36. int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm);
  37. void __scm_destroy(struct scm_cookie *scm);
  38. struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl);
  39. #ifdef CONFIG_SECURITY_NETWORK
  40. static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm)
  41. {
  42. security_socket_getpeersec_dgram(sock, NULL, &scm->secid);
  43. }
  44. #else
  45. static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm)
  46. { }
  47. #endif /* CONFIG_SECURITY_NETWORK */
  48. static __inline__ void scm_set_cred(struct scm_cookie *scm,
  49. struct pid *pid, kuid_t uid, kgid_t gid)
  50. {
  51. scm->pid = get_pid(pid);
  52. scm->creds.pid = pid_vnr(pid);
  53. scm->creds.uid = uid;
  54. scm->creds.gid = gid;
  55. }
  56. static __inline__ void scm_destroy_cred(struct scm_cookie *scm)
  57. {
  58. put_pid(scm->pid);
  59. scm->pid = NULL;
  60. }
  61. static __inline__ void scm_destroy(struct scm_cookie *scm)
  62. {
  63. scm_destroy_cred(scm);
  64. if (scm->fp)
  65. __scm_destroy(scm);
  66. }
  67. static __inline__ int scm_send(struct socket *sock, struct msghdr *msg,
  68. struct scm_cookie *scm, bool forcecreds)
  69. {
  70. memset(scm, 0, sizeof(*scm));
  71. scm->creds.uid = INVALID_UID;
  72. scm->creds.gid = INVALID_GID;
  73. if (forcecreds)
  74. scm_set_cred(scm, task_tgid(current), current_uid(), current_gid());
  75. unix_get_peersec_dgram(sock, scm);
  76. if (msg->msg_controllen <= 0)
  77. return 0;
  78. return __scm_send(sock, msg, scm);
  79. }
  80. #ifdef CONFIG_SECURITY_NETWORK
  81. static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm)
  82. {
  83. char *secdata;
  84. u32 seclen;
  85. int err;
  86. if (test_bit(SOCK_PASSSEC, &sock->flags)) {
  87. err = security_secid_to_secctx(scm->secid, &secdata, &seclen);
  88. if (!err) {
  89. put_cmsg(msg, SOL_SOCKET, SCM_SECURITY, seclen, secdata);
  90. security_release_secctx(secdata, seclen);
  91. }
  92. }
  93. }
  94. #else
  95. static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm)
  96. { }
  97. #endif /* CONFIG_SECURITY_NETWORK */
  98. static __inline__ void scm_recv(struct socket *sock, struct msghdr *msg,
  99. struct scm_cookie *scm, int flags)
  100. {
  101. if (!msg->msg_control) {
  102. if (test_bit(SOCK_PASSCRED, &sock->flags) || scm->fp)
  103. msg->msg_flags |= MSG_CTRUNC;
  104. scm_destroy(scm);
  105. return;
  106. }
  107. if (test_bit(SOCK_PASSCRED, &sock->flags)) {
  108. struct user_namespace *current_ns = current_user_ns();
  109. struct ucred ucreds = {
  110. .pid = scm->creds.pid,
  111. .uid = from_kuid_munged(current_ns, scm->creds.uid),
  112. .gid = from_kgid_munged(current_ns, scm->creds.gid),
  113. };
  114. put_cmsg(msg, SOL_SOCKET, SCM_CREDENTIALS, sizeof(ucreds), &ucreds);
  115. }
  116. scm_destroy_cred(scm);
  117. scm_passec(sock, msg, scm);
  118. if (!scm->fp)
  119. return;
  120. scm_detach_fds(msg, scm);
  121. }
  122. #endif /* __LINUX_NET_SCM_H */