zzqq0103

Untitled

Dec 1st, 2024
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 KB | None | 0 0
  1. #define _GNU_SOURCE
  2.  
  3. #include <arpa/inet.h>
  4. #include <endian.h>
  5. #include <errno.h>
  6. #include <net/if.h>
  7. #include <netinet/in.h>
  8. #include <stdbool.h>
  9. #include <stdint.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/socket.h>
  14. #include <sys/syscall.h>
  15. #include <sys/types.h>
  16. #include <unistd.h>
  17.  
  18. #include <linux/genetlink.h>
  19. #include <linux/if_addr.h>
  20. #include <linux/if_link.h>
  21. #include <linux/in6.h>
  22. #include <linux/neighbour.h>
  23. #include <linux/net.h>
  24. #include <linux/netlink.h>
  25. #include <linux/rtnetlink.h>
  26. #include <linux/veth.h>
  27.  
  28. struct nlmsg {
  29. char* pos;
  30. int nesting;
  31. struct nlattr* nested[8];
  32. char buf[4096];
  33. };
  34.  
  35. static void netlink_init(struct nlmsg* nlmsg, int typ, int flags,
  36. const void* data, int size)
  37. {
  38. memset(nlmsg, 0, sizeof(*nlmsg));
  39. struct nlmsghdr* hdr = (struct nlmsghdr*)nlmsg->buf;
  40. hdr->nlmsg_type = typ;
  41. hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
  42. memcpy(hdr + 1, data, size);
  43. nlmsg->pos = (char*)(hdr + 1) + NLMSG_ALIGN(size);
  44. }
  45.  
  46. static void netlink_attr(struct nlmsg* nlmsg, int typ, const void* data,
  47. int size)
  48. {
  49. struct nlattr* attr = (struct nlattr*)nlmsg->pos;
  50. attr->nla_len = sizeof(*attr) + size;
  51. attr->nla_type = typ;
  52. if (size > 0)
  53. memcpy(attr + 1, data, size);
  54. nlmsg->pos += NLMSG_ALIGN(attr->nla_len);
  55. }
  56.  
  57. static int netlink_send_ext(struct nlmsg* nlmsg, int sock, uint16_t reply_type,
  58. int* reply_len, bool dofail)
  59. {
  60. if (nlmsg->pos > nlmsg->buf + sizeof(nlmsg->buf) || nlmsg->nesting)
  61. exit(1);
  62. struct nlmsghdr* hdr = (struct nlmsghdr*)nlmsg->buf;
  63. hdr->nlmsg_len = nlmsg->pos - nlmsg->buf;
  64. struct sockaddr_nl addr;
  65. memset(&addr, 0, sizeof(addr));
  66. addr.nl_family = AF_NETLINK;
  67. ssize_t n = sendto(sock, nlmsg->buf, hdr->nlmsg_len, 0,
  68. (struct sockaddr*)&addr, sizeof(addr));
  69. if (n != (ssize_t)hdr->nlmsg_len) {
  70. if (dofail)
  71. exit(1);
  72. return -1;
  73. }
  74. n = recv(sock, nlmsg->buf, sizeof(nlmsg->buf), 0);
  75. if (reply_len)
  76. *reply_len = 0;
  77. if (n < 0) {
  78. if (dofail)
  79. exit(1);
  80. return -1;
  81. }
  82. if (n < (ssize_t)sizeof(struct nlmsghdr)) {
  83. errno = EINVAL;
  84. if (dofail)
  85. exit(1);
  86. return -1;
  87. }
  88. if (hdr->nlmsg_type == NLMSG_DONE)
  89. return 0;
  90. if (reply_len && hdr->nlmsg_type == reply_type) {
  91. *reply_len = n;
  92. return 0;
  93. }
  94. if (n < (ssize_t)(sizeof(struct nlmsghdr) + sizeof(struct nlmsgerr))) {
  95. errno = EINVAL;
  96. if (dofail)
  97. exit(1);
  98. return -1;
  99. }
  100. if (hdr->nlmsg_type != NLMSG_ERROR) {
  101. errno = EINVAL;
  102. if (dofail)
  103. exit(1);
  104. return -1;
  105. }
  106. errno = -((struct nlmsgerr*)(hdr + 1))->error;
  107. return -errno;
  108. }
  109.  
  110. static int netlink_query_family_id(struct nlmsg* nlmsg, int sock,
  111. const char* family_name, bool dofail)
  112. {
  113. struct genlmsghdr genlhdr;
  114. memset(&genlhdr, 0, sizeof(genlhdr));
  115. genlhdr.cmd = CTRL_CMD_GETFAMILY;
  116. netlink_init(nlmsg, GENL_ID_CTRL, 0, &genlhdr, sizeof(genlhdr));
  117. netlink_attr(nlmsg, CTRL_ATTR_FAMILY_NAME, family_name,
  118. strnlen(family_name, GENL_NAMSIZ - 1) + 1);
  119. int n = 0;
  120. int err = netlink_send_ext(nlmsg, sock, GENL_ID_CTRL, &n, dofail);
  121. if (err < 0) {
  122. return -1;
  123. }
  124. uint16_t id = 0;
  125. struct nlattr* attr = (struct nlattr*)(nlmsg->buf + NLMSG_HDRLEN +
  126. NLMSG_ALIGN(sizeof(genlhdr)));
  127. for (; (char*)attr < nlmsg->buf + n;
  128. attr = (struct nlattr*)((char*)attr + NLMSG_ALIGN(attr->nla_len))) {
  129. if (attr->nla_type == CTRL_ATTR_FAMILY_ID) {
  130. id = *(uint16_t*)(attr + 1);
  131. break;
  132. }
  133. }
  134. if (!id) {
  135. errno = EINVAL;
  136. return -1;
  137. }
  138. recv(sock, nlmsg->buf, sizeof(nlmsg->buf), 0);
  139. return id;
  140. }
  141.  
  142. const int kInitNetNsFd = 201;
  143.  
  144. static long syz_init_net_socket(volatile long domain, volatile long type,
  145. volatile long proto)
  146. {
  147. return syscall(__NR_socket, domain, type, proto);
  148. }
  149.  
  150. static long syz_genetlink_get_family_id(volatile long name,
  151. volatile long sock_arg)
  152. {
  153. int fd = sock_arg;
  154. if (fd < 0) {
  155. fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
  156. if (fd == -1) {
  157. return -1;
  158. }
  159. }
  160. struct nlmsg nlmsg_tmp;
  161. int ret = netlink_query_family_id(&nlmsg_tmp, fd, (char*)name, false);
  162. if ((int)sock_arg < 0)
  163. close(fd);
  164. if (ret < 0) {
  165. return -1;
  166. }
  167. return ret;
  168. }
  169.  
  170. uint64_t r[2] = {0xffffffffffffffff, 0x0};
  171.  
  172. int main(void)
  173. {
  174. syscall(__NR_mmap, /*addr=*/0x1ffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
  175. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
  176. /*offset=*/0ul);
  177. syscall(__NR_mmap, /*addr=*/0x20000000ul, /*len=*/0x1000000ul,
  178. /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul,
  179. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
  180. /*offset=*/0ul);
  181. syscall(__NR_mmap, /*addr=*/0x21000000ul, /*len=*/0x1000ul, /*prot=*/0ul,
  182. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
  183. /*offset=*/0ul);
  184. const char* reason;
  185. (void)reason;
  186. intptr_t res = 0;
  187. if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {
  188. }
  189. res = -1;
  190. res = syz_init_net_socket(/*domain=*/0x10, /*type=*/3, /*proto=*/0x10);
  191. if (res != -1)
  192. r[0] = res;
  193. memcpy((void*)0x20000340, "NLBL_UNLBL\000", 11);
  194. res = -1;
  195. res = syz_genetlink_get_family_id(/*name=*/0x20000340, /*fd=*/r[0]);
  196. if (res != -1)
  197. r[1] = res;
  198. *(uint64_t*)0x20000480 = 0;
  199. *(uint32_t*)0x20000488 = 0;
  200. *(uint64_t*)0x20000490 = 0x20000440;
  201. *(uint64_t*)0x20000440 = 0x20000000;
  202. *(uint32_t*)0x20000000 = 0x6c;
  203. *(uint16_t*)0x20000004 = r[1];
  204. *(uint16_t*)0x20000006 = 1;
  205. *(uint32_t*)0x20000008 = 0x70bd29;
  206. *(uint32_t*)0x2000000c = 0x25dfdbfb;
  207. *(uint8_t*)0x20000010 = 6;
  208. *(uint8_t*)0x20000011 = 0;
  209. *(uint16_t*)0x20000012 = 0;
  210. *(uint16_t*)0x20000014 = 0x14;
  211. *(uint16_t*)0x20000016 = 2;
  212. *(uint8_t*)0x20000018 = 0xfe;
  213. *(uint8_t*)0x20000019 = 0x80;
  214. memset((void*)0x2000001a, 0, 13);
  215. *(uint8_t*)0x20000027 = 0xbb;
  216. *(uint16_t*)0x20000028 = 8;
  217. *(uint16_t*)0x2000002a = 4;
  218. *(uint32_t*)0x2000002c = htobe32(0x7f000001);
  219. *(uint16_t*)0x20000030 = 0x14;
  220. *(uint16_t*)0x20000032 = 3;
  221. *(uint8_t*)0x20000034 = -1;
  222. *(uint8_t*)0x20000035 = 2;
  223. memset((void*)0x20000036, 0, 13);
  224. *(uint8_t*)0x20000043 = 1;
  225. *(uint16_t*)0x20000044 = 0x26;
  226. *(uint16_t*)0x20000046 = 7;
  227. memcpy((void*)0x20000048, "system_u:object_r:cron_spool_t:s0\000", 34);
  228. *(uint64_t*)0x20000448 = 0x6c;
  229. *(uint64_t*)0x20000498 = 1;
  230. *(uint64_t*)0x200004a0 = 0;
  231. *(uint64_t*)0x200004a8 = 0;
  232. *(uint32_t*)0x200004b0 = 0x4814;
  233. syscall(__NR_sendmsg, /*fd=*/r[0], /*msg=*/0x20000480ul,
  234. /*f=MSG_ZEROCOPY|MSG_NOSIGNAL*/ 0x4004000ul);
  235. return 0;
  236. }
  237.  
Advertisement
Add Comment
Please, Sign In to add comment