valgrindtest.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that: (1) source code distributions
  7. * retain the above copyright notice and this paragraph in its entirety, (2)
  8. * distributions including binary code include the above copyright notice and
  9. * this paragraph in its entirety in the documentation or other materials
  10. * provided with the distribution, and (3) all advertising materials mentioning
  11. * features or use of this software display the following acknowledgement:
  12. * ``This product includes software developed by the University of California,
  13. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14. * the University nor the names of its contributors may be used to endorse
  15. * or promote products derived from this software without specific prior
  16. * written permission.
  17. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. */
  21. #include "varattrs.h"
  22. /*
  23. * This doesn't actually test libpcap itself; it tests whether
  24. * valgrind properly handles the APIs libpcap uses. If it doesn't,
  25. * we end up getting patches submitted to "fix" references that
  26. * valgrind claims are being made to uninitialized data, when, in
  27. * fact, the OS isn't making any such references - or we get
  28. * valgrind *not* detecting *actual* incorrect references.
  29. *
  30. * Both BPF and Linux socket filters aren't handled correctly
  31. * by some versions of valgrind. See valgrind bug 318203 for
  32. * Linux:
  33. *
  34. * https://bugs.kde.org/show_bug.cgi?id=318203
  35. *
  36. * and valgrind bug 312989 for macOS:
  37. *
  38. * https://bugs.kde.org/show_bug.cgi?id=312989
  39. *
  40. * The fixes for both of those are checked into the official valgrind
  41. * repository.
  42. *
  43. * The unofficial FreeBSD port has similar issues to the official macOS
  44. * port, for similar reasons.
  45. */
  46. #ifndef lint
  47. static const char copyright[] _U_ =
  48. "@(#) Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000\n\
  49. The Regents of the University of California. All rights reserved.\n";
  50. #endif
  51. #ifdef HAVE_CONFIG_H
  52. #include <config.h>
  53. #endif
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include <stdarg.h>
  58. #include <unistd.h>
  59. #include <fcntl.h>
  60. #include <errno.h>
  61. #include <arpa/inet.h>
  62. #include <sys/types.h>
  63. #include <sys/stat.h>
  64. #include "pcap/funcattrs.h"
  65. #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(_AIX) || defined(sun)
  66. /* OS with BPF - use BPF */
  67. #define USE_BPF
  68. #elif defined(linux)
  69. /* Linux - use socket filters */
  70. #define USE_SOCKET_FILTERS
  71. #else
  72. #error "Unknown platform or platform that doesn't support Valgrind"
  73. #endif
  74. #if defined(USE_BPF)
  75. #include <sys/ioctl.h>
  76. #include <net/bpf.h>
  77. /*
  78. * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
  79. * native OS version, as we're going to be doing our own ioctls to
  80. * make sure that, in the uninitialized-data tests, the filters aren't
  81. * checked by libpcap before being handed to BPF.
  82. */
  83. #define PCAP_DONT_INCLUDE_PCAP_BPF_H
  84. #elif defined(USE_SOCKET_FILTERS)
  85. #include <sys/socket.h>
  86. #include <linux/types.h>
  87. #include <linux/filter.h>
  88. #endif
  89. #include <pcap.h>
  90. static char *program_name;
  91. /* Forwards */
  92. static void PCAP_NORETURN usage(void);
  93. static void PCAP_NORETURN error(const char *, ...) PCAP_PRINTFLIKE(1, 2);
  94. static void warning(const char *, ...) PCAP_PRINTFLIKE(1, 2);
  95. /*
  96. * On Windows, we need to open the file in binary mode, so that
  97. * we get all the bytes specified by the size we get from "fstat()".
  98. * On UNIX, that's not necessary. O_BINARY is defined on Windows;
  99. * we define it as 0 if it's not defined, so it does nothing.
  100. */
  101. #ifndef O_BINARY
  102. #define O_BINARY 0
  103. #endif
  104. static char *
  105. read_infile(char *fname)
  106. {
  107. register int i, fd, cc;
  108. register char *cp;
  109. struct stat buf;
  110. fd = open(fname, O_RDONLY|O_BINARY);
  111. if (fd < 0)
  112. error("can't open %s: %s", fname, pcap_strerror(errno));
  113. if (fstat(fd, &buf) < 0)
  114. error("can't stat %s: %s", fname, pcap_strerror(errno));
  115. cp = malloc((u_int)buf.st_size + 1);
  116. if (cp == NULL)
  117. error("malloc(%d) for %s: %s", (u_int)buf.st_size + 1,
  118. fname, pcap_strerror(errno));
  119. cc = read(fd, cp, (u_int)buf.st_size);
  120. if (cc < 0)
  121. error("read %s: %s", fname, pcap_strerror(errno));
  122. if (cc != buf.st_size)
  123. error("short read %s (%d != %d)", fname, cc, (int)buf.st_size);
  124. close(fd);
  125. /* replace "# comment" with spaces */
  126. for (i = 0; i < cc; i++) {
  127. if (cp[i] == '#')
  128. while (i < cc && cp[i] != '\n')
  129. cp[i++] = ' ';
  130. }
  131. cp[cc] = '\0';
  132. return (cp);
  133. }
  134. /* VARARGS */
  135. static void
  136. error(const char *fmt, ...)
  137. {
  138. va_list ap;
  139. (void)fprintf(stderr, "%s: ", program_name);
  140. va_start(ap, fmt);
  141. (void)vfprintf(stderr, fmt, ap);
  142. va_end(ap);
  143. if (*fmt) {
  144. fmt += strlen(fmt);
  145. if (fmt[-1] != '\n')
  146. (void)fputc('\n', stderr);
  147. }
  148. exit(1);
  149. /* NOTREACHED */
  150. }
  151. /* VARARGS */
  152. static void
  153. warning(const char *fmt, ...)
  154. {
  155. va_list ap;
  156. (void)fprintf(stderr, "%s: WARNING: ", program_name);
  157. va_start(ap, fmt);
  158. (void)vfprintf(stderr, fmt, ap);
  159. va_end(ap);
  160. if (*fmt) {
  161. fmt += strlen(fmt);
  162. if (fmt[-1] != '\n')
  163. (void)fputc('\n', stderr);
  164. }
  165. }
  166. /*
  167. * Copy arg vector into a new buffer, concatenating arguments with spaces.
  168. */
  169. static char *
  170. copy_argv(register char **argv)
  171. {
  172. register char **p;
  173. register u_int len = 0;
  174. char *buf;
  175. char *src, *dst;
  176. p = argv;
  177. if (*p == 0)
  178. return 0;
  179. while (*p)
  180. len += strlen(*p++) + 1;
  181. buf = (char *)malloc(len);
  182. if (buf == NULL)
  183. error("copy_argv: malloc");
  184. p = argv;
  185. dst = buf;
  186. while ((src = *p++) != NULL) {
  187. while ((*dst++ = *src++) != '\0')
  188. ;
  189. dst[-1] = ' ';
  190. }
  191. dst[-1] = '\0';
  192. return buf;
  193. }
  194. #define INSN_COUNT 17
  195. int
  196. main(int argc, char **argv)
  197. {
  198. char *cp, *device;
  199. int op;
  200. int dorfmon, useactivate;
  201. char ebuf[PCAP_ERRBUF_SIZE];
  202. char *infile;
  203. const char *cmdbuf;
  204. pcap_if_t *devlist;
  205. pcap_t *pd;
  206. int status = 0;
  207. int pcap_fd;
  208. #if defined(USE_BPF)
  209. struct bpf_program bad_fcode;
  210. struct bpf_insn uninitialized[INSN_COUNT];
  211. #elif defined(USE_SOCKET_FILTERS)
  212. struct sock_fprog bad_fcode;
  213. struct sock_filter uninitialized[INSN_COUNT];
  214. #endif
  215. struct bpf_program fcode;
  216. device = NULL;
  217. dorfmon = 0;
  218. useactivate = 0;
  219. infile = NULL;
  220. if ((cp = strrchr(argv[0], '/')) != NULL)
  221. program_name = cp + 1;
  222. else
  223. program_name = argv[0];
  224. opterr = 0;
  225. while ((op = getopt(argc, argv, "aF:i:I")) != -1) {
  226. switch (op) {
  227. case 'a':
  228. useactivate = 1;
  229. break;
  230. case 'F':
  231. infile = optarg;
  232. break;
  233. case 'i':
  234. device = optarg;
  235. break;
  236. case 'I':
  237. dorfmon = 1;
  238. useactivate = 1; /* required for rfmon */
  239. break;
  240. default:
  241. usage();
  242. /* NOTREACHED */
  243. }
  244. }
  245. if (device == NULL) {
  246. /*
  247. * No interface specified; get whatever pcap_lookupdev()
  248. * finds.
  249. */
  250. if (pcap_findalldevs(&devlist, ebuf) == -1)
  251. error("%s", ebuf);
  252. if (devlist == NULL)
  253. error("no interfaces available for capture");
  254. device = strdup(devlist->name);
  255. pcap_freealldevs(devlist);
  256. }
  257. if (infile != NULL) {
  258. /*
  259. * Filter specified with "-F" and a file containing
  260. * a filter.
  261. */
  262. cmdbuf = read_infile(infile);
  263. } else {
  264. if (optind < argc) {
  265. /*
  266. * Filter specified with arguments on the
  267. * command line.
  268. */
  269. cmdbuf = copy_argv(&argv[optind+1]);
  270. } else {
  271. /*
  272. * No filter specified; use an empty string, which
  273. * compiles to an "accept all" filter.
  274. */
  275. cmdbuf = "";
  276. }
  277. }
  278. if (useactivate) {
  279. pd = pcap_create(device, ebuf);
  280. if (pd == NULL)
  281. error("%s: pcap_create() failed: %s", device, ebuf);
  282. status = pcap_set_snaplen(pd, 65535);
  283. if (status != 0)
  284. error("%s: pcap_set_snaplen failed: %s",
  285. device, pcap_statustostr(status));
  286. status = pcap_set_promisc(pd, 1);
  287. if (status != 0)
  288. error("%s: pcap_set_promisc failed: %s",
  289. device, pcap_statustostr(status));
  290. if (dorfmon) {
  291. status = pcap_set_rfmon(pd, 1);
  292. if (status != 0)
  293. error("%s: pcap_set_rfmon failed: %s",
  294. device, pcap_statustostr(status));
  295. }
  296. status = pcap_set_timeout(pd, 1000);
  297. if (status != 0)
  298. error("%s: pcap_set_timeout failed: %s",
  299. device, pcap_statustostr(status));
  300. status = pcap_activate(pd);
  301. if (status < 0) {
  302. /*
  303. * pcap_activate() failed.
  304. */
  305. error("%s: %s\n(%s)", device,
  306. pcap_statustostr(status), pcap_geterr(pd));
  307. } else if (status > 0) {
  308. /*
  309. * pcap_activate() succeeded, but it's warning us
  310. * of a problem it had.
  311. */
  312. warning("%s: %s\n(%s)", device,
  313. pcap_statustostr(status), pcap_geterr(pd));
  314. }
  315. } else {
  316. *ebuf = '\0';
  317. pd = pcap_open_live(device, 65535, 1, 1000, ebuf);
  318. if (pd == NULL)
  319. error("%s", ebuf);
  320. else if (*ebuf)
  321. warning("%s", ebuf);
  322. }
  323. pcap_fd = pcap_fileno(pd);
  324. /*
  325. * Try setting a filter with an uninitialized bpf_program
  326. * structure. This should cause valgrind to report a
  327. * problem.
  328. *
  329. * We don't check for errors, because it could get an
  330. * error due to a bad pointer or count.
  331. */
  332. #if defined(USE_BPF)
  333. ioctl(pcap_fd, BIOCSETF, &bad_fcode);
  334. #elif defined(USE_SOCKET_FILTERS)
  335. setsockopt(pcap_fd, SOL_SOCKET, SO_ATTACH_FILTER, &bad_fcode,
  336. sizeof(bad_fcode));
  337. #endif
  338. /*
  339. * Try setting a filter with an initialized bpf_program
  340. * structure that points to an uninitialized program.
  341. * That should also cause valgrind to report a problem.
  342. *
  343. * We don't check for errors, because it could get an
  344. * error due to a bad pointer or count.
  345. */
  346. #if defined(USE_BPF)
  347. bad_fcode.bf_len = INSN_COUNT;
  348. bad_fcode.bf_insns = uninitialized;
  349. ioctl(pcap_fd, BIOCSETF, &bad_fcode);
  350. #elif defined(USE_SOCKET_FILTERS)
  351. bad_fcode.len = INSN_COUNT;
  352. bad_fcode.filter = uninitialized;
  353. setsockopt(pcap_fd, SOL_SOCKET, SO_ATTACH_FILTER, &bad_fcode,
  354. sizeof(bad_fcode));
  355. #endif
  356. /*
  357. * Now compile a filter and set the filter with that.
  358. * That should *not* cause valgrind to report a
  359. * problem.
  360. */
  361. if (pcap_compile(pd, &fcode, cmdbuf, 1, 0) < 0)
  362. error("can't compile filter: %s", pcap_geterr(pd));
  363. if (pcap_setfilter(pd, &fcode) < 0)
  364. error("can't set filter: %s", pcap_geterr(pd));
  365. pcap_close(pd);
  366. exit(status < 0 ? 1 : 0);
  367. }
  368. static void
  369. usage(void)
  370. {
  371. (void)fprintf(stderr, "%s, with %s\n", program_name,
  372. pcap_lib_version());
  373. (void)fprintf(stderr,
  374. "Usage: %s [-aI] [ -F file ] [ -I interface ] [ expression ]\n",
  375. program_name);
  376. exit(1);
  377. }