scm.h 3.5 KB

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