1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/param.h>
33 #include <sys/mman.h>
34 #include <sys/stat.h>
35
36 #include <capsicum_helpers.h>
37 #include <err.h>
38 #include <limits.h>
39 #include <signal.h>
40 #include <stdbool.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <unistd.h>
44
45 #include "extern.h"
46
47 static u_char *remmap(u_char *, int, off_t);
48 static void segv_handler(int);
49 #define MMAP_CHUNK (8*1024*1024)
50
51 #define ROUNDPAGE(i) ((i) & ~pagemask)
52
53 int
c_regular(int fd1,const char * file1,off_t skip1,off_t len1,int fd2,const char * file2,off_t skip2,off_t len2,off_t limit)54 c_regular(int fd1, const char *file1, off_t skip1, off_t len1,
55 int fd2, const char *file2, off_t skip2, off_t len2, off_t limit)
56 {
57 struct sigaction act, oact;
58 cap_rights_t rights;
59 u_char ch, *p1, *p2, *m1, *m2, *e1, *e2;
60 off_t byte, length, line;
61 off_t pagemask, off1, off2;
62 size_t pagesize;
63 int dfound;
64
65 if (skip1 > len1) {
66 eofmsg(file1);
67 return (DIFF_EXIT);
68 }
69 len1 -= skip1;
70 if (skip2 > len2) {
71 eofmsg(file2);
72 return (DIFF_EXIT);
73 }
74 len2 -= skip2;
75
76 if (sflag && len1 != len2)
77 return (DIFF_EXIT);
78
79 pagesize = getpagesize();
80 pagemask = (off_t)pagesize - 1;
81 off1 = ROUNDPAGE(skip1);
82 off2 = ROUNDPAGE(skip2);
83
84 length = MIN(len1, len2);
85 if (limit > 0)
86 length = MIN(length, limit);
87
88 if ((m1 = remmap(NULL, fd1, off1)) == NULL) {
89 return (c_special(fd1, file1, skip1, fd2, file2, skip2, limit));
90 }
91
92 if ((m2 = remmap(NULL, fd2, off2)) == NULL) {
93 munmap(m1, MMAP_CHUNK);
94 return (c_special(fd1, file1, skip1, fd2, file2, skip2, limit));
95 }
96
97 if (caph_rights_limit(fd1, cap_rights_init(&rights, CAP_MMAP_R)) < 0)
98 err(1, "unable to limit rights for %s", file1);
99 if (caph_rights_limit(fd2, cap_rights_init(&rights, CAP_MMAP_R)) < 0)
100 err(1, "unable to limit rights for %s", file2);
101 if (caph_enter() < 0)
102 err(ERR_EXIT, "unable to enter capability mode");
103
104 sigemptyset(&act.sa_mask);
105 act.sa_flags = SA_NODEFER;
106 act.sa_handler = segv_handler;
107 if (sigaction(SIGSEGV, &act, &oact))
108 err(ERR_EXIT, "sigaction()");
109
110 dfound = 0;
111 e1 = m1 + MMAP_CHUNK;
112 e2 = m2 + MMAP_CHUNK;
113 p1 = m1 + (skip1 - off1);
114 p2 = m2 + (skip2 - off2);
115
116 for (byte = line = 1; length--; ++byte) {
117 #ifdef SIGINFO
118 if (info) {
119 (void)fprintf(stderr, "%s %s char %zu line %zu\n",
120 file1, file2, (size_t)byte, (size_t)line);
121 info = 0;
122 }
123 #endif
124 if ((ch = *p1) != *p2) {
125 dfound = 1;
126 if (xflag) {
127 (void)printf("%08llx %02x %02x\n",
128 (long long)byte - 1, ch, *p2);
129 } else if (lflag) {
130 if (bflag)
131 (void)printf("%6lld %3o %c %3o %c\n",
132 (long long)byte, ch, ch, *p2, *p2);
133 else
134 (void)printf("%6lld %3o %3o\n",
135 (long long)byte, ch, *p2);
136 } else {
137 diffmsg(file1, file2, byte, line, ch, *p2);
138 return (DIFF_EXIT);
139 }
140 }
141 if (ch == '\n')
142 ++line;
143 if (++p1 == e1) {
144 off1 += MMAP_CHUNK;
145 if ((p1 = m1 = remmap(m1, fd1, off1)) == NULL) {
146 munmap(m2, MMAP_CHUNK);
147 err(ERR_EXIT, "remmap %s", file1);
148 }
149 e1 = m1 + MMAP_CHUNK;
150 }
151 if (++p2 == e2) {
152 off2 += MMAP_CHUNK;
153 if ((p2 = m2 = remmap(m2, fd2, off2)) == NULL) {
154 munmap(m1, MMAP_CHUNK);
155 err(ERR_EXIT, "remmap %s", file2);
156 }
157 e2 = m2 + MMAP_CHUNK;
158 }
159 }
160 munmap(m1, MMAP_CHUNK);
161 munmap(m2, MMAP_CHUNK);
162
163 if (sigaction(SIGSEGV, &oact, NULL))
164 err(ERR_EXIT, "sigaction()");
165
166 if (len1 != len2) {
167 eofmsg(len1 > len2 ? file2 : file1);
168 return (DIFF_EXIT);
169 }
170 return (dfound ? DIFF_EXIT : 0);
171 }
172
173 static u_char *
remmap(u_char * mem,int fd,off_t offset)174 remmap(u_char *mem, int fd, off_t offset)
175 {
176 if (mem != NULL)
177 munmap(mem, MMAP_CHUNK);
178 mem = mmap(NULL, MMAP_CHUNK, PROT_READ, MAP_SHARED, fd, offset);
179 if (mem == MAP_FAILED)
180 return (NULL);
181 madvise(mem, MMAP_CHUNK, MADV_SEQUENTIAL);
182 return (mem);
183 }
184
185 static void
segv_handler(int sig __unused)186 segv_handler(int sig __unused) {
187 static const char msg[] = "cmp: Input/output error (caught SIGSEGV)\n";
188
189 write(STDERR_FILENO, msg, sizeof(msg));
190 _exit(EXIT_FAILURE);
191 }
192