scm.h 3.5 KB

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