]> FriiDump Source - friidump.git/blob - src/linux_rawio.c
Harden GCC-4243N A102 HLDS E7 profile
[friidump.git] / src / linux_rawio.c
1 #include "linux_rawio.h"
2
3 #include <errno.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8
9 friidump_linux_rawio_state friidump_linux_rawio_state_from_status_file(
10     const char *status_path,
11     unsigned long long *effective_mask) {
12     FILE *status;
13     char line[256];
14     unsigned long long value;
15     char *end;
16
17     if (effective_mask)
18         *effective_mask = 0;
19
20     if (!status_path || !status_path[0])
21         return FRIIDUMP_LINUX_RAWIO_UNKNOWN;
22
23     status = fopen(status_path, "rb");
24     if (!status)
25         return FRIIDUMP_LINUX_RAWIO_UNKNOWN;
26
27     while (fgets(line, sizeof(line), status)) {
28         if (strncmp(line, "CapEff:", 7) != 0)
29             continue;
30
31         errno = 0;
32         value = strtoull(line + 7, &end, 16);
33         fclose(status);
34
35         if (errno != 0 || end == line + 7)
36             return FRIIDUMP_LINUX_RAWIO_UNKNOWN;
37
38         if (effective_mask)
39             *effective_mask = value;
40
41         return (value & (1ULL << FRIIDUMP_LINUX_CAP_SYS_RAWIO))
42             ? FRIIDUMP_LINUX_RAWIO_PRESENT
43             : FRIIDUMP_LINUX_RAWIO_MISSING;
44     }
45
46     fclose(status);
47     return FRIIDUMP_LINUX_RAWIO_UNKNOWN;
48 }
49
50 friidump_linux_rawio_state friidump_linux_rawio_effective(
51     unsigned long long *effective_mask) {
52     return friidump_linux_rawio_state_from_status_file(
53         "/proc/self/status", effective_mask);
54 }
55
56 int friidump_linux_executable_path(char *buffer, size_t buffer_size) {
57     ssize_t length;
58
59     if (!buffer || buffer_size < 2)
60         return 0;
61
62     buffer[0] = '\0';
63     length = readlink("/proc/self/exe", buffer, buffer_size - 1);
64     if (length <= 0 || (size_t) length >= buffer_size)
65         return 0;
66
67     buffer[length] = '\0';
68     return 1;
69 }