scm.h 3.4 KB

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