1 /* $NetBSD: t_memmem.c,v 1.6 2020/11/27 16:50:02 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Perry E. Metzger of Metzger, Dowdeswell & Co. LLC.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <atf-c.h>
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/mman.h>
38
39 char p0[] = "";
40 int lp0 = 0;
41 char p1[] = "0123";
42 int lp1 = 4;
43 char p2[] = "456";
44 int lp2 = 3;
45 char p3[] = "789";
46 int lp3 = 3;
47 char p4[] = "abc";
48 int lp4 = 3;
49 char p5[] = "0";
50 int lp5 = 1;
51 char p6[] = "9";
52 int lp6 = 1;
53 char p7[] = "654";
54 int lp7 = 3;
55 char p8[] = "89abc";
56 int lp8 = 5;
57
58 char b0[] = "";
59 int lb0 = 0;
60 char b1[] = "0";
61 int lb1 = 1;
62 char b2[] = "0123456789";
63 int lb2 = 10;
64
65 #define expect(b) \
66 if (!(b)) { \
67 fprintf(stderr, "failed on line %d\n", __LINE__); \
68 atf_tc_fail("Check stderr for test id/line"); \
69 }
70
71 ATF_TC(memmem_basic);
ATF_TC_HEAD(memmem_basic,tc)72 ATF_TC_HEAD(memmem_basic, tc)
73 {
74
75 atf_tc_set_md_var(tc, "descr", "Test memmem results");
76 }
77
ATF_TC_BODY(memmem_basic,tc)78 ATF_TC_BODY(memmem_basic, tc)
79 {
80
81 expect(memmem(b2, lb2, p0, lp0) == b2);
82 expect(memmem(b0, lb0, p0, lp0) == b0);
83 expect(memmem(b0, lb0, p1, lp1) == NULL);
84 expect(memmem(b1, lb1, p1, lp1) == NULL);
85
86 expect(memmem(b2, lb2, p1, lp1) == b2);
87 expect(memmem(b2, lb2, p2, lp2) == (b2 + 4));
88 expect(memmem(b2, lb2, p3, lp3) == (b2 + 7));
89
90 expect(memmem(b2, lb2, p5, lp5) == b2);
91 expect(memmem(b2, lb2, p6, lp6) == (b2 + 9));
92
93 expect(memmem(b2, lb2, p4, lp4) == NULL);
94 expect(memmem(b2, lb2, p7, lp7) == NULL);
95 expect(memmem(b2, lb2, p8, lp8) == NULL);
96 }
97
98 ATF_TC(memmem_oob);
ATF_TC_HEAD(memmem_oob,tc)99 ATF_TC_HEAD(memmem_oob, tc)
100 {
101 atf_tc_set_md_var(tc, "descr", "Test memmem out of bounds read");
102 }
103
ATF_TC_BODY(memmem_oob,tc)104 ATF_TC_BODY(memmem_oob, tc)
105 {
106 static const char str[] = "abcde";
107 size_t pg = getpagesize();
108 char *src = mmap(NULL, 2 * pg, PROT_READ|PROT_WRITE,
109 MAP_ANON|MAP_PRIVATE, -1, (off_t)0);
110 ATF_CHECK(src != MAP_FAILED);
111 char *guard = mmap(src + pg, pg,
112 PROT_NONE, MAP_ANON|MAP_PRIVATE|MAP_FIXED, -1, (off_t)0);
113 printf("%p\n", guard);
114 for (size_t i = 2; i < 5; i++) {
115 char *search = src + pg - i;
116 char match[sizeof(str)];
117 search[-1] = str[0];
118 search[0] = str[0];
119 search[1] = str[0];
120 memcpy(match, str, i);
121 ATF_CHECK(memmem(search, i, match, i) != search);
122 }
123 munmap(guard, pg);
124 munmap(src, pg);
125 }
126
127
ATF_TP_ADD_TCS(tp)128 ATF_TP_ADD_TCS(tp)
129 {
130
131 ATF_TP_ADD_TC(tp, memmem_basic);
132 ATF_TP_ADD_TC(tp, memmem_oob);
133
134 return atf_no_error();
135 }
136