qaqaq

Untitled

Aug 10th, 2022
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.30 KB | None | 0 0
  1. // autogenerated by syzkaller (https://github.com/google/syzkaller)
  2.  
  3. #define _GNU_SOURCE
  4. #include <linux/usb/ch9.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <usbg/function/hid.h>
  8. #include <usbg/function/loopback.h>
  9. #include <usbg/function/midi.h>
  10. #include <usbg/function/ms.h>
  11. #include <usbg/function/net.h>
  12. #include <usbg/function/printer.h>
  13. #include <usbg/usbg.h>
  14.  
  15. #define MAX_FUNC_NUM 2
  16. #define MAX_DEVICE_NUM 8
  17.  
  18. union usbg_function_attr {
  19. int default_attr;
  20. struct usbg_f_midi_attrs midi_attr;
  21. struct usbg_f_ms_attrs ms_attr;
  22. struct usbg_f_net_attrs net_attr;
  23. struct usbg_f_printer_attrs printer_attr;
  24. struct usbg_f_loopback_attrs loopback_attr;
  25. };
  26.  
  27. struct usbg_func_config {
  28. usbg_function_type f_type;
  29. union usbg_function_attr f_attrs;
  30. };
  31.  
  32. struct usb_gadget_device {
  33. struct usbg_gadget_attrs* g_attrs;
  34. struct usbg_config_attrs* c_attrs;
  35. int func_num;
  36. struct usbg_func_config func_conf[MAX_FUNC_NUM];
  37. };
  38.  
  39. struct usb_gadget_device usb_device[MAX_DEVICE_NUM];
  40.  
  41. struct usbg_gadget_strs g_strs = {
  42. .manufacturer = (char*)"Foo Inc.",
  43. .product = (char*)"Bar Gadget",
  44. .serial = (char*)"12345678"};
  45.  
  46. struct usbg_config_strs c_strs = {
  47. .configuration = (char*)"1xconf"};
  48.  
  49. static volatile long syz_attach_gadget_impl(struct usb_gadget_device* dev, int uid)
  50. {
  51. usbg_state* s;
  52. usbg_gadget* g;
  53. usbg_config* c;
  54. usbg_function* f[MAX_FUNC_NUM];
  55. usbg_udc* u;
  56.  
  57. int ret = -1;
  58. int usbg_ret;
  59.  
  60. char g_name[10];
  61. sprintf(g_name, "g%d", uid);
  62.  
  63. usbg_ret = usbg_init("/sys/kernel/config", &s);
  64. if (usbg_ret != USBG_SUCCESS) {
  65. fprintf(stderr, "Error on usbg init\n");
  66. fprintf(stderr, "Error: %s : %s\n", usbg_error_name((usbg_error)usbg_ret),
  67. usbg_strerror((usbg_error)usbg_ret));
  68. goto out1;
  69. }
  70.  
  71. usbg_ret = usbg_create_gadget(s, g_name, dev->g_attrs, &g_strs, &g);
  72. if (usbg_ret != USBG_SUCCESS) {
  73. fprintf(stderr, "Error on creating gadget\n");
  74. fprintf(stderr, "Error: %s : %s\n", usbg_error_name((usbg_error)usbg_ret),
  75. usbg_strerror((usbg_error)usbg_ret));
  76. goto out2;
  77. }
  78.  
  79. for (int i = 0; i < dev->func_num; i++) {
  80. char f_name[10];
  81. sprintf(f_name, "func%d", i);
  82.  
  83. if (dev->func_conf[i].f_attrs.default_attr == 0xffff)
  84. usbg_ret = usbg_create_function(g, dev->func_conf[i].f_type, (char*)f_name, NULL, &f[i]);
  85. else
  86. usbg_ret = usbg_create_function(g, dev->func_conf[i].f_type, (char*)f_name, &(dev->func_conf[i].f_attrs), &f[i]);
  87. if (usbg_ret != USBG_SUCCESS) {
  88. fprintf(stderr, "Error on creating gadget func\n");
  89. fprintf(stderr, "Error: %s : %s\n", usbg_error_name((usbg_error)usbg_ret),
  90. usbg_strerror((usbg_error)usbg_ret));
  91. goto out2;
  92. }
  93. }
  94.  
  95. usbg_ret = usbg_create_config(g, 1, "The only one config", dev->c_attrs, &c_strs, &c);
  96. if (usbg_ret != USBG_SUCCESS) {
  97. fprintf(stderr, "Error on creating gadget config\n");
  98. fprintf(stderr, "Error: %s : %s\n", usbg_error_name((usbg_error)usbg_ret),
  99. usbg_strerror((usbg_error)usbg_ret));
  100. goto out2;
  101. }
  102.  
  103. for (int i = 0; i < dev->func_num; i++) {
  104. char f_name[10];
  105. sprintf(f_name, "f_name.%d", i);
  106. usbg_ret = usbg_add_config_function(c, (char*)f_name, f[i]);
  107.  
  108. if (usbg_ret != USBG_SUCCESS) {
  109. fprintf(stderr, "Error on adding func to config\n");
  110. fprintf(stderr, "Error: %s : %s\n", usbg_error_name((usbg_error)usbg_ret),
  111. usbg_strerror((usbg_error)usbg_ret));
  112. goto out2;
  113. }
  114. }
  115.  
  116. u = usbg_get_first_udc(s);
  117. if (uid > 0) {
  118. for (int i = 0; i < uid; i++) {
  119. u = usbg_get_next_udc(u);
  120. }
  121. }
  122. usbg_ret = usbg_enable_gadget(g, u);
  123. if (usbg_ret != USBG_SUCCESS) {
  124. fprintf(stderr, "Error on enabling udc\n");
  125. fprintf(stderr, "Error: %s : %s\n", usbg_error_name((usbg_error)usbg_ret),
  126. usbg_strerror((usbg_error)usbg_ret));
  127. goto out2;
  128. }
  129.  
  130. ret = 0;
  131.  
  132. out2:
  133. usbg_cleanup(s);
  134.  
  135. out1:
  136. return ret;
  137. }
  138.  
  139. static int remove_gadget(usbg_gadget* g)
  140. {
  141. int usbg_ret;
  142. usbg_udc* u;
  143.  
  144. u = usbg_get_gadget_udc(g);
  145.  
  146. if (u) {
  147. usbg_ret = usbg_disable_gadget(g);
  148. if (usbg_ret != USBG_SUCCESS) {
  149. fprintf(stderr, "Error on disable gadget udc\n");
  150. fprintf(stderr, "Error: %s : %s\n", usbg_error_name((usbg_error)usbg_ret),
  151. usbg_strerror((usbg_error)usbg_ret));
  152. goto out;
  153. }
  154. }
  155.  
  156. usbg_ret = usbg_rm_gadget(g, USBG_RM_RECURSE);
  157. if (usbg_ret != USBG_SUCCESS) {
  158. fprintf(stderr, "Error on gadget remove\n");
  159. fprintf(stderr, "Error: %s : %s\n", usbg_error_name((usbg_error)usbg_ret),
  160. usbg_strerror((usbg_error)usbg_ret));
  161. }
  162.  
  163. out:
  164. return usbg_ret;
  165. }
  166.  
  167. static volatile long syz_detach_gadget_impl(int uid)
  168. {
  169. int usbg_ret;
  170. int ret = -1;
  171. usbg_state* s;
  172. usbg_gadget* g;
  173. const char* g_name;
  174. char g_name_target[10];
  175. sprintf(g_name_target, "g%d", uid);
  176.  
  177. usbg_ret = usbg_init("/sys/kernel/config", &s);
  178. if (usbg_ret != USBG_SUCCESS) {
  179. fprintf(stderr, "Error on USB state init\n");
  180. fprintf(stderr, "Error: %s : %s\n", usbg_error_name((usbg_error)usbg_ret),
  181. usbg_strerror((usbg_error)usbg_ret));
  182. goto out1;
  183. }
  184.  
  185. g = usbg_get_first_gadget(s);
  186. while (g != NULL) {
  187. g_name = usbg_get_gadget_name(g);
  188.  
  189. if (strcmp(g_name, g_name_target) == 0) {
  190. usbg_gadget* g_next = usbg_get_next_gadget(g);
  191.  
  192. usbg_ret = remove_gadget(g);
  193. if (usbg_ret != USBG_SUCCESS)
  194. goto out2;
  195.  
  196. g = g_next;
  197. } else {
  198. g = usbg_get_next_gadget(g);
  199. }
  200. }
  201. usleep(500000);
  202. ret = 0;
  203.  
  204. out2:
  205. usbg_cleanup(s);
  206. out1:
  207. return ret;
  208. }
  209.  
  210. static void parse_dev_descriptors(const char* buffer, struct usb_gadget_device* dev)
  211. {
  212. memset(dev, 0, sizeof(*dev));
  213.  
  214. dev->g_attrs = (struct usbg_gadget_attrs*)buffer;
  215. dev->c_attrs = (struct usbg_config_attrs*)(buffer + sizeof(struct usbg_gadget_attrs));
  216. dev->func_num = *(int*)(buffer + sizeof(struct usbg_gadget_attrs) + sizeof(struct usbg_config_attrs) + sizeof(int16_t));
  217. int start_attr = sizeof(struct usbg_gadget_attrs) + sizeof(struct usbg_config_attrs) + sizeof(int16_t) + 2 * sizeof(int32_t);
  218. int conf_size = 40;
  219. printf("conf_size: %x\n", conf_size);
  220. for (int i = 0; i < dev->func_num; i++) {
  221. dev->func_conf[i] = *(struct usbg_func_config*)(buffer + start_attr + i * conf_size);
  222. }
  223. }
  224.  
  225. static volatile long syz_attach_gadget(volatile long a0, volatile long a1)
  226. {
  227. const char* dev = (const char*)a0;
  228. uint64_t uid = a1;
  229. parse_dev_descriptors(dev, &usb_device[uid]);
  230. return syz_attach_gadget_impl(&usb_device[uid], uid);
  231. }
  232.  
  233. static volatile long syz_detach_gadget(volatile long a0)
  234. {
  235. int uid = a0;
  236. return syz_detach_gadget_impl(uid);
  237. }
  238. #include <endian.h>
  239. #include <stdint.h>
  240. #include <stdio.h>
  241. #include <stdlib.h>
  242. #include <string.h>
  243. #include <sys/syscall.h>
  244. #include <sys/types.h>
  245. #include <unistd.h>
  246.  
  247. int main(void)
  248. {
  249. syscall(__NR_mmap, 0x1ffff000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul);
  250. syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x32ul, -1, 0ul);
  251. syscall(__NR_mmap, 0x21000000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul);
  252.  
  253. syz_detach_gadget(0);
  254. *(uint16_t*)0x20000040 = 0x200;
  255. *(uint8_t*)0x20000042 = 0;
  256. *(uint8_t*)0x20000043 = 0;
  257. *(uint8_t*)0x20000044 = 0;
  258. *(uint8_t*)0x20000045 = 0;
  259. *(uint16_t*)0x20000046 = 0x5d9;
  260. *(uint16_t*)0x20000048 = 0xa225;
  261. *(uint16_t*)0x2000004a = 0;
  262. *(uint8_t*)0x2000004c = 0x80;
  263. *(uint8_t*)0x2000004d = 0xdc;
  264. *(uint32_t*)0x20000050 = 2;
  265. *(uint8_t*)0x20000058 = 0xa;
  266. *(uint32_t*)0x20000060 = 0xffff;
  267. *(uint8_t*)0x20000080 = 0xb;
  268. *(uint32_t*)0x20000088 = 4;
  269. *(uint64_t*)0x20000090 = 0x20000000;
  270. *(uint8_t*)0x20000000 = 9;
  271. *(uint32_t*)0x20000098 = 1;
  272. *(uint32_t*)0x2000009c = 9;
  273. *(uint32_t*)0x200000a0 = 0x8001;
  274. *(uint32_t*)0x200000a4 = 0x8001;
  275. syz_attach_gadget(0x20000040, 0);
  276. return 0;
  277. }
  278.  
Add Comment
Please, Sign In to add comment