Debian Codegraph @

src/OVAL/probes/fsdev.c

raw symbols SCIP
1 /**
2 * @file fsdev.c
3 * @brief fsdev API implementation
4 * @author "Daniel Kopecek" <[email protected]>
5 *
6 */
7 /*
8 * Copyright 2009 Red Hat Inc., Durham, North Carolina.
9 * All Rights Reserved.
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 * Authors:
26 * "Daniel Kopecek" <[email protected]>
27 */
28
29 #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:409:9`!
30 #include <config.h>cxx . . $ `<file>/build/config.h`/
31 #endif
32
33 #include <stdio.h>
34 #include <stddef.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <stdlib.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <errno.h>
41
42 #if defined(OS_LINUXcxx . . $ `compat/oscap_platforms.h:38:10`!)
43 # include <mntent.h>
44 # include <unistd.h>
45 # ifndef _PATH_MOUNTEDcxx . . $ `<external>/11492533085529128233/paths.h:55:9`!
46 # define _PATH_MOUNTED MOUNTED
47 # endif
48 #elif defined(OS_SOLARIS)
49 # include <sys/mnttab.h>
50 # include <sys/mntent.h>
51 # include <sys/unistd.h>
52 #elif defined(OS_FREEBSD) || defined(OS_APPLE)
53 # include <sys/param.h>
54 # include <sys/ucred.h>
55 # include <sys/mount.h>
56 #elif defined(OS_AIX)
57 # include <unistd.h>
58 # include <mntent.h>
59 # include <fshelp.h>
60 # include <sys/vfs.h>
61 # include <sys/vmount.h>
62 # define _PATH_MOUNTED MOUNTED
63 #else
64 # error "Sorry, your OS isn't supported."
65 #endif
66
67 #include "fsdev.h"cxx . . $ `<file>/src/OVAL/probes/fsdev.h`/
68 #include "common/util.h"cxx . . $ `<file>/src/common/util.h`/
69
70 /**
71 * Compare two dev_t variables.
72 */
73 static int fsdev_cmpcxx . . $ fsdev_cmp(9148c8c3ff42c9d8).(const void *a, const void *b)
74 {
75 return memcmpcxx . . $ memcmp(36f949851efa3055).(a, b, sizeof(dev_tcxx . . $ dev_t#));
76 }
77
78 #if defined(OS_LINUXcxx . . $ `compat/oscap_platforms.h:38:10`!) || defined(OS_AIX)
79
80 #define DEVID_ARRAY_SIZEcxx . . $ `src/OVAL/probes/fsdev.c:80:9`! 16
81 #define DEVID_ARRAY_ADDcxx . . $ `src/OVAL/probes/fsdev.c:81:9`! 8
82
83 #if defined(OS_LINUXcxx . . $ `compat/oscap_platforms.h:38:10`!)
84 static int is_local_fscxx . . $ is_local_fs(fcaa7379d1a310f9).(struct mntentcxx . . $ mntent# *ment)
85 {
86 /*
87 * When type of the filesystem is autofs, it means the mtab entry
88 * describes the autofs configuration, which means ment->mnt_fsname
89 * is a path to the relevant autofs map, eg. /etc/auto.misc. In this
90 * situation, the following code which analyses ment->mnt_type would
91 * not work. When the filesystem handled by autofs is mounted, there
92 * is another different entry in mtab which contains the real block
93 * special device or remote filesystem in ment->mnt_fsname, and that
94 * will be parsed in a different call of this function.
95 */
96 if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(ment->mnt_typecxx . . $ mntent#mnt_type., "autofs")) {
97 return 0;
98 }
99
100 /*
101 * The following code is inspired by systemd, function fstype_is_network:
102 * https://github.com/systemd/systemd/blob/21fd6bc263f49b57867d90d2e1f9f255e5509134/src/basic/mountpoint-util.c#L290
103 */
104
105 const char *fstype = ment->mnt_typecxx . . $ mntent#mnt_type.;
106 if (oscap_str_startswithcxx . . $ oscap_str_startswith(b7a7aa4a824b7595).(fstype, "fuse.")) {
107 fstype += strlencxx . . $ strlen(751346f8406fd082).("fuse.");
108 }
109 // OVAL's idea behind 'local' is to follow the 'df -l' behaviour
110 const char *pseudo_fs[] = {
111 "proc",
112 "sysfs",
113 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!
114 };
115 const char *network_fs[] = {
116 "afs",
117 "auristorfs",
118 "ceph",
119 "cifs",
120 "smb3",
121 "smbfs",
122 "sshfs",
123 "ncpfs",
124 "ncp",
125 "nfs",
126 "nfs4",
127 "gfs",
128 "gfs2",
129 "glusterfs",
130 "gpfs",
131 "pvfs2", /* OrangeFS */
132 "ocfs2",
133 "lustre",
134 "davfs",
135 "panfs",
136 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!
137 };
138 for (int i = 0; pseudo_fs[i]; i++) {
139 if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(pseudo_fs[i], fstype))
140 return 0;
141 }
142 for (int i = 0; network_fs[i]; i++) {
143 if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(network_fs[i], fstype))
144 return 0;
145 }
146 return 1;
147 }
148
149 #elif defined(OS_AIX)
150 static int is_local_fs(struct mntent *ment)
151 {
152 int i;
153 struct vfs_ent *e;
154
155 static const int remote_fs_types[] = {
156 MNT_NFS,
157 MNT_NFS3,
158 MNT_AUTOFS, /* remote? */
159 MNT_NFS4,
160 MNT_RFS4,
161 MNT_CIFS,
162 MNT_BADVFS /* end mark */
163 };
164
165 for (i = 0; remote_fs_types[i] != MNT_BADVFS; i++) {
166 e = getvfsbytype(remote_fs_types[i]);
167 if (e != NULL
168 && e->vfsent_name != NULL
169 && strcmp(ment->mnt_type, e->vfsent_name) == 0) {
170 return 0;
171 }
172 }
173
174 return 1;
175 }
176
177 #endif /* OS_AIX */
178
179 static fsdev_tcxx . . $ fsdev_t# *__fsdev_initcxx . . $ __fsdev_init(62b706092a0cdfa3).(fsdev_tcxx . . $ fsdev_t# *lfs)
180 {
181 int e;
182 FILEcxx . . $ FILE# *fp;
183 size_t i;
184
185 struct mntentcxx . . $ mntent# *ment;
186 struct statcxx . . $ stat# st;
187
188 fp = setmntentcxx . . $ setmntent(82ec0787cd37e0a3).(_PATH_MOUNTEDcxx . . $ `<external>/11492533085529128233/paths.h:55:9`!, "r");
189 if (fp == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
190 e = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
191 freecxx . . $ free(5d22bdacc48458e8).(lfs);
192 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = e;
193 return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
194 }
195
196 lfs->idscxx . . $ $anonymous_type_9ae834c88293f1e5_0#ids. = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(dev_tcxx . . $ dev_t#) * DEVID_ARRAY_SIZEcxx . . $ `src/OVAL/probes/fsdev.c:80:9`!);
197
198 if (lfs->idscxx . . $ $anonymous_type_9ae834c88293f1e5_0#ids. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
199 e = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
200 freecxx . . $ free(5d22bdacc48458e8).(lfs);
201 endmntentcxx . . $ endmntent(89bc1efcdac0ca22).(fp);
202 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = e;
203 return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
204 }
205
206 lfs->cntcxx . . $ $anonymous_type_9ae834c88293f1e5_0#cnt. = DEVID_ARRAY_SIZEcxx . . $ `src/OVAL/probes/fsdev.c:80:9`!;
207 i = 0;
208
209 while ((ment = getmntentcxx . . $ getmntent(7289d64343998259).(fp)) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
210 if (!is_local_fscxx . . $ is_local_fs(fcaa7379d1a310f9).(ment))
211 continue;
212 if (statcxx . . $ stat(a9cd1e23e96f423c).(ment->mnt_dircxx . . $ mntent#mnt_dir., &st) != 0)
213 continue;
214 if (i >= lfs->cntcxx . . $ $anonymous_type_9ae834c88293f1e5_0#cnt.) {
215 lfs->cntcxx . . $ $anonymous_type_9ae834c88293f1e5_0#cnt. += DEVID_ARRAY_ADDcxx . . $ `src/OVAL/probes/fsdev.c:81:9`!;
216 void *new_ids = realloccxx . . $ realloc(417ab160f6baadc6).(lfs->idscxx . . $ $anonymous_type_9ae834c88293f1e5_0#ids., sizeof(dev_tcxx . . $ dev_t#) * lfs->cntcxx . . $ $anonymous_type_9ae834c88293f1e5_0#cnt.);
217 if (new_ids == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
218 e = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
219 freecxx . . $ free(5d22bdacc48458e8).(lfs->idscxx . . $ $anonymous_type_9ae834c88293f1e5_0#ids.);
220 freecxx . . $ free(5d22bdacc48458e8).(lfs);
221 endmntentcxx . . $ endmntent(89bc1efcdac0ca22).(fp);
222 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = e;
223 return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
224 }
225 lfs->idscxx . . $ $anonymous_type_9ae834c88293f1e5_0#ids. = new_ids;
226 }
227 memcpycxx . . $ memcpy(cc817cb175f282d).(&(lfs->idscxx . . $ $anonymous_type_9ae834c88293f1e5_0#ids.[i++]), &st.st_devcxx . . $ stat#st_dev., sizeof(dev_tcxx . . $ dev_t#));
228 }
229
230 endmntentcxx . . $ endmntent(89bc1efcdac0ca22).(fp);
231
232 void *new_ids = realloccxx . . $ realloc(417ab160f6baadc6).(lfs->idscxx . . $ $anonymous_type_9ae834c88293f1e5_0#ids., sizeof(dev_tcxx . . $ dev_t#) * i);
233 if (new_ids == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! && i > 0) {
234 e = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
235 freecxx . . $ free(5d22bdacc48458e8).(lfs->idscxx . . $ $anonymous_type_9ae834c88293f1e5_0#ids.);
236 freecxx . . $ free(5d22bdacc48458e8).(lfs);
237 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = e;
238 return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
239 }
240 lfs->idscxx . . $ $anonymous_type_9ae834c88293f1e5_0#ids. = new_ids;
241 lfs->cntcxx . . $ $anonymous_type_9ae834c88293f1e5_0#cnt. = i;
242
243 return (lfs);
244 }
245
246 #elif defined(OS_FREEBSD) || defined(OS_APPLE)
247 static fsdev_t *__fsdev_init(fsdev_t *lfs)
248 {
249 struct statfs *mntbuf = NULL;
250 struct stat st;
251 size_t i;
252 int e;
253
254 lfs->cnt = getmntinfo(&mntbuf, MNT_NOWAIT);
255 lfs->ids = malloc(sizeof(dev_t) * lfs->cnt);
256
257 for (i = 0; i < lfs->cnt; ++i) {
258 if (stat(mntbuf[i].f_mntonname, &st) != 0)
259 continue;
260
261 memcpy(&(lfs->ids[i]), &st.st_dev, sizeof(dev_t));
262 }
263
264 if (i != lfs->cnt) {
265 void *new_ids = realloc(lfs->ids, sizeof(dev_t) * i);
266 if (new_ids == NULL) {
267 e = errno;
268 free(lfs->ids);
269 free(lfs);
270 errno = e;
271 return (NULL);
272 }
273 lfs->ids = new_ids;
274 lfs->cnt = i;
275 }
276
277 return (lfs);
278 }
279 #elif defined(OS_SOLARIS)
280
281 #define DEVID_ARRAY_SIZE 16
282 #define DEVID_ARRAY_ADD 8
283
284 static fsdev_t *__fsdev_init(fsdev_t *lfs)
285 {
286 int e;
287 FILE *fp;
288 size_t i;
289
290 struct mnttab mentbuf;
291 struct stat st;
292
293 fp = fopen(MNTTAB, "r");
294 if (fp == NULL) {
295 e = errno;
296 free(lfs);
297 errno = e;
298 return (NULL);
299 }
300
301 lfs->ids = malloc(sizeof(dev_t) * DEVID_ARRAY_SIZE);
302
303 if (lfs->ids == NULL) {
304 e = errno;
305 free(lfs);
306 fclose(fp);
307 errno = e;
308 return (NULL);
309 }
310
311 lfs->cnt = DEVID_ARRAY_SIZE;
312 i = 0;
313
314 while ((getmntent(fp, &mentbuf)) == 0) {
315 /* TODO: Is this check reliable? */
316 if (stat(mentbuf.mnt_special, &st) == 0 && (st.st_mode & S_IFCHR)) {
317
318 if (i >= lfs->cnt) {
319 lfs->cnt += DEVID_ARRAY_ADD;
320 void *new_ids = realloc(lfs->ids, sizeof(dev_t) * lfs->cnt);
321 if (new_ids == NULL) {
322 e = errno;
323 free(lfs->ids);
324 free(lfs);
325 fclose(fp);
326 errno = e;
327 return (NULL);
328 }
329 lfs->ids = new_ids;
330 }
331
332 memcpy(&(lfs->ids[i++]), &st.st_dev, sizeof(dev_t));
333 }
334 }
335
336 fclose(fp);
337
338 void *new_ids = realloc(lfs->ids, sizeof(dev_t) * i);
339 if (new_ids == NULL) {
340 e = errno;
341 free(lfs->ids);
342 free(lfs);
343 errno = e;
344 return (NULL);
345 }
346 lfs->ids = new_ids;
347 lfs->cnt = i;
348
349 return (lfs);
350 }
351 #endif
352
353 fsdev_tcxx . . $ fsdev_t# *fsdev_initcxx . . $ fsdev_init(7d22408f4c7e197c).()
354 {
355 fsdev_tcxx . . $ fsdev_t# *lfs;
356
357 lfs = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(fsdev_tcxx . . $ fsdev_t#));
358
359 if (lfs == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
360 return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
361
362 if (__fsdev_initcxx . . $ __fsdev_init(62b706092a0cdfa3).(lfs) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
363 return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
364
365 if (lfs->idscxx . . $ $anonymous_type_9ae834c88293f1e5_0#ids. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! && lfs->cntcxx . . $ $anonymous_type_9ae834c88293f1e5_0#cnt. > 1)
366 qsortcxx . . $ qsort(ebb60b13a65e77d7).(lfs->idscxx . . $ $anonymous_type_9ae834c88293f1e5_0#ids., lfs->cntcxx . . $ $anonymous_type_9ae834c88293f1e5_0#cnt., sizeof(dev_tcxx . . $ dev_t#), fsdev_cmpcxx . . $ fsdev_cmp(9148c8c3ff42c9d8).);
367
368 return (lfs);
369 }
370
371 void fsdev_freecxx . . $ fsdev_free(27e1c92ef469a2f9).(fsdev_tcxx . . $ fsdev_t# * lfs)
372 {
373 if (lfs != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
374 freecxx . . $ free(5d22bdacc48458e8).(lfs->idscxx . . $ $anonymous_type_9ae834c88293f1e5_0#ids.);
375 freecxx . . $ free(5d22bdacc48458e8).(lfs);
376 }
377 return;
378 }
379
380 int fsdev_searchcxx . . $ fsdev_search(a29fa8ec2853d098).(fsdev_tcxx . . $ fsdev_t# * lfs, void *id)
381 {
382 uint16_tcxx . . $ uint16_t# w, s;
383 int cmp;
384
385 if (!lfs)
386 return 1;
387
388 w = lfs->cntcxx . . $ $anonymous_type_9ae834c88293f1e5_0#cnt.;
389 s = 0;
390
391 while (w > 0) {
392 cmp = fsdev_cmpcxx . . $ fsdev_cmp(9148c8c3ff42c9d8).(id, &(lfs->idscxx . . $ $anonymous_type_9ae834c88293f1e5_0#ids.[s + w / 2]));
393 if (cmp > 0) {
394 s += w / 2 + 1;
395 w = w - w / 2 - 1;
396 } else if (cmp < 0) {
397 w = w / 2;
398 } else {
399 return (1);
400 }
401 }
402
403 return (0);
404 }
405
406 int fsdev_pathcxx . . $ fsdev_path(f173a51f6dfb99a9).(fsdev_tcxx . . $ fsdev_t# * lfs, const char *path)
407 {
408 struct statcxx . . $ stat# st;
409
410 if (statcxx . . $ stat(a9cd1e23e96f423c).(path, &st) != 0)
411 return (-1);
412
413 return fsdev_searchcxx . . $ fsdev_search(a29fa8ec2853d098).(lfs, &st.st_devcxx . . $ stat#st_dev.);
414 }
415
416 int fsdev_fdcxx . . $ fsdev_fd(4b26416c454d026a).(fsdev_tcxx . . $ fsdev_t# * lfs, int fd)
417 {
418 struct statcxx . . $ stat# st;
419
420 if (fstatcxx . . $ fstat(a43660ce2f600cb5).(fd, &st) != 0)
421 return (-1);
422
423 return fsdev_searchcxx . . $ fsdev_search(a29fa8ec2853d098).(lfs, &st.st_devcxx . . $ stat#st_dev.);
424 }