uevent.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <cutils/uevent.h>
  17. #include <errno.h>
  18. #include <stdbool.h>
  19. #include <string.h>
  20. #include <strings.h>
  21. #include <sys/socket.h>
  22. #include <sys/un.h>
  23. #include <unistd.h>
  24. #include <linux/netlink.h>
  25. /**
  26. * Like recv(), but checks that messages actually originate from the kernel.
  27. */
  28. ssize_t uevent_kernel_multicast_recv(int socket, void *buffer, size_t length)
  29. {
  30. uid_t user = -1;
  31. return uevent_kernel_multicast_uid_recv(socket, buffer, length, &user);
  32. }
  33. /**
  34. * Like the above, but passes a uid_t in by reference. In the event that this
  35. * fails due to a bad uid check, the uid_t will be set to the uid of the
  36. * socket's peer.
  37. *
  38. * If this method rejects a netlink message from outside the kernel, it
  39. * returns -1, sets errno to EIO, and sets "user" to the UID associated with the
  40. * message. If the peer UID cannot be determined, "user" is set to -1."
  41. */
  42. ssize_t uevent_kernel_multicast_uid_recv(int socket, void *buffer,
  43. size_t length, uid_t *user)
  44. {
  45. struct iovec iov = { buffer, length };
  46. struct sockaddr_nl addr;
  47. char control[CMSG_SPACE(sizeof(struct ucred))];
  48. struct msghdr hdr = {
  49. &addr,
  50. sizeof(addr),
  51. &iov,
  52. 1,
  53. control,
  54. sizeof(control),
  55. 0,
  56. };
  57. *user = -1;
  58. ssize_t n = recvmsg(socket, &hdr, 0);
  59. if (n <= 0) {
  60. return n;
  61. }
  62. struct cmsghdr *cmsg = CMSG_FIRSTHDR(&hdr);
  63. if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
  64. /* ignoring netlink message with no sender credentials */
  65. goto out;
  66. }
  67. struct ucred *cred = (struct ucred *)CMSG_DATA(cmsg);
  68. *user = cred->uid;
  69. if (cred->uid != 0) {
  70. /* ignoring netlink message from non-root user */
  71. goto out;
  72. }
  73. if (addr.nl_groups == 0 || addr.nl_pid != 0) {
  74. /* ignoring non-kernel or unicast netlink message */
  75. goto out;
  76. }
  77. return n;
  78. out:
  79. /* clear residual potentially malicious data */
  80. bzero(buffer, length);
  81. errno = EIO;
  82. return -1;
  83. }
  84. int uevent_open_socket(int buf_sz, bool passcred)
  85. {
  86. struct sockaddr_nl addr;
  87. int on = passcred;
  88. int s;
  89. memset(&addr, 0, sizeof(addr));
  90. addr.nl_family = AF_NETLINK;
  91. addr.nl_pid = getpid();
  92. addr.nl_groups = 0xffffffff;
  93. s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
  94. if(s < 0)
  95. return -1;
  96. setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &buf_sz, sizeof(buf_sz));
  97. setsockopt(s, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
  98. if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  99. close(s);
  100. return -1;
  101. }
  102. return s;
  103. }