diff options
author | smitsohu <smitsohu@gmail.com> | 2019-06-26 01:49:30 +0200 |
---|---|---|
committer | smitsohu <smitsohu@gmail.com> | 2019-06-26 01:49:30 +0200 |
commit | 1e3c4ba8531e7edfbde0d22e2b78107314283a4a (patch) | |
tree | 0227b272f04edf5ca4bebaf16e7683a3aa5c7fef /src | |
parent | run cppcheck in gitlab-ci (diff) | |
download | firejail-1e3c4ba8531e7edfbde0d22e2b78107314283a4a.tar.gz firejail-1e3c4ba8531e7edfbde0d22e2b78107314283a4a.tar.zst firejail-1e3c4ba8531e7edfbde0d22e2b78107314283a4a.zip |
simplify octal esc conversion, minor adjustments
Diffstat (limited to 'src')
-rw-r--r-- | src/firejail/mountinfo.c | 65 |
1 files changed, 28 insertions, 37 deletions
diff --git a/src/firejail/mountinfo.c b/src/firejail/mountinfo.c index 7369ad247..42e160dec 100644 --- a/src/firejail/mountinfo.c +++ b/src/firejail/mountinfo.c | |||
@@ -33,7 +33,7 @@ static MountData mdata; | |||
33 | 33 | ||
34 | // Convert octal escape sequence to decimal value | 34 | // Convert octal escape sequence to decimal value |
35 | static int read_oct(const char *path) { | 35 | static int read_oct(const char *path) { |
36 | int decimal = 0; | 36 | int dec = 0; |
37 | int digit, i; | 37 | int digit, i; |
38 | // there are always exactly three octal digits | 38 | // there are always exactly three octal digits |
39 | for (i = 1; i < 4; i++) { | 39 | for (i = 1; i < 4; i++) { |
@@ -42,29 +42,26 @@ static int read_oct(const char *path) { | |||
42 | fprintf(stderr, "Error: cannot read /proc/self/mountinfo\n"); | 42 | fprintf(stderr, "Error: cannot read /proc/self/mountinfo\n"); |
43 | exit(1); | 43 | exit(1); |
44 | } | 44 | } |
45 | decimal = (decimal + digit - '0') * 8; | 45 | dec = (dec << 3) + (digit - '0'); |
46 | } | 46 | } |
47 | decimal /= 8; | 47 | return dec; |
48 | return decimal; | ||
49 | } | 48 | } |
50 | 49 | ||
51 | // Restore empty spaces in pathnames extracted from /proc/self/mountinfo | 50 | // Restore empty spaces in pathnames extracted from /proc/self/mountinfo |
52 | static void unmangle_path(char *path) { | 51 | static void unmangle_path(char *path) { |
53 | char *p = strchr(path, '\\'); | 52 | char *p = strchr(path, '\\'); |
54 | if (p) { | 53 | if (p && read_oct(p) == ' ') { |
55 | if (read_oct(p) == ' ') { | 54 | *p = ' '; |
56 | *p = ' '; | 55 | int i = 3; |
57 | int i = 3; | 56 | do { |
58 | do { | 57 | p++; |
59 | p++; | 58 | if (*(p + i) == '\\' && read_oct(p + i) == ' ') { |
60 | if (*(p + i) == '\\' && read_oct(p + i) == ' ') { | 59 | *p = ' '; |
61 | *p = ' '; | 60 | i += 3; |
62 | i += 3; | 61 | } |
63 | } | 62 | else |
64 | else | 63 | *p = *(p + i); |
65 | *p = *(p + i); | 64 | } while (*p); |
66 | } while (*p); | ||
67 | } | ||
68 | } | 65 | } |
69 | } | 66 | } |
70 | 67 | ||
@@ -136,7 +133,6 @@ MountData *get_last_mount(void) { | |||
136 | // open /proc/self/mountinfo | 133 | // open /proc/self/mountinfo |
137 | FILE *fp = fopen("/proc/self/mountinfo", "re"); | 134 | FILE *fp = fopen("/proc/self/mountinfo", "re"); |
138 | if (!fp) { | 135 | if (!fp) { |
139 | perror("fopen"); | ||
140 | fprintf(stderr, "Error: cannot read /proc/self/mountinfo\n"); | 136 | fprintf(stderr, "Error: cannot read /proc/self/mountinfo\n"); |
141 | exit(1); | 137 | exit(1); |
142 | } | 138 | } |
@@ -167,31 +163,24 @@ int get_mount_id(const char *path) { | |||
167 | if (asprintf(&fdinfo, "/proc/self/fdinfo/%d", fd) == -1) | 163 | if (asprintf(&fdinfo, "/proc/self/fdinfo/%d", fd) == -1) |
168 | errExit("asprintf"); | 164 | errExit("asprintf"); |
169 | FILE *fp = fopen(fdinfo, "re"); | 165 | FILE *fp = fopen(fdinfo, "re"); |
170 | if (!fp) { | 166 | free(fdinfo); |
171 | perror("fopen"); | 167 | if (!fp) |
172 | fprintf(stderr, "Error: cannot open %s\n", fdinfo); | 168 | goto errexit; |
173 | exit(1); | ||
174 | } | ||
175 | 169 | ||
176 | // read the file | 170 | // read the file |
177 | char buf[MAX_BUF]; | 171 | char buf[MAX_BUF]; |
178 | if (fgets(buf, MAX_BUF, fp) == NULL) { | 172 | if (fgets(buf, MAX_BUF, fp) == NULL) |
179 | fprintf(stderr, "Error: cannot read %s\n", fdinfo); | 173 | goto errexit; |
180 | exit(1); | ||
181 | } | ||
182 | do { | 174 | do { |
183 | if (strncmp(buf, "mnt_id:", 7) == 0) { | 175 | if (strncmp(buf, "mnt_id:", 7) == 0) { |
184 | char *ptr = buf + 7; | 176 | char *ptr = buf + 7; |
185 | while (*ptr != '\0' && (*ptr == ' ' || *ptr == '\t')) { | 177 | while (*ptr != '\0' && (*ptr == ' ' || *ptr == '\t')) { |
186 | ptr++; | 178 | ptr++; |
187 | } | 179 | } |
188 | if (*ptr == '\0') { | 180 | if (*ptr == '\0') |
189 | fprintf(stderr, "Error: cannot read %s\n", fdinfo); | 181 | goto errexit; |
190 | exit(1); | ||
191 | } | ||
192 | fclose(fp); | 182 | fclose(fp); |
193 | close(fd); | 183 | close(fd); |
194 | free(fdinfo); | ||
195 | return atoi(ptr); | 184 | return atoi(ptr); |
196 | } | 185 | } |
197 | } while (fgets(buf, MAX_BUF, fp)); | 186 | } while (fgets(buf, MAX_BUF, fp)); |
@@ -199,8 +188,11 @@ int get_mount_id(const char *path) { | |||
199 | // fallback, kernels older than 3.15 don't expose the mount id in this place | 188 | // fallback, kernels older than 3.15 don't expose the mount id in this place |
200 | fclose(fp); | 189 | fclose(fp); |
201 | close(fd); | 190 | close(fd); |
202 | free(fdinfo); | ||
203 | return -2; | 191 | return -2; |
192 | |||
193 | errexit: | ||
194 | fprintf(stderr, "Error: cannot read proc file\n"); | ||
195 | exit(1); | ||
204 | } | 196 | } |
205 | 197 | ||
206 | // Check /proc/self/mountinfo if path contains any mounts points. | 198 | // Check /proc/self/mountinfo if path contains any mounts points. |
@@ -211,7 +203,6 @@ char **build_mount_array(const int mount_id, const char *path) { | |||
211 | // open /proc/self/mountinfo | 203 | // open /proc/self/mountinfo |
212 | FILE *fp = fopen("/proc/self/mountinfo", "re"); | 204 | FILE *fp = fopen("/proc/self/mountinfo", "re"); |
213 | if (!fp) { | 205 | if (!fp) { |
214 | perror("fopen"); | ||
215 | fprintf(stderr, "Error: cannot read /proc/self/mountinfo\n"); | 206 | fprintf(stderr, "Error: cannot read /proc/self/mountinfo\n"); |
216 | exit(1); | 207 | exit(1); |
217 | } | 208 | } |
@@ -245,8 +236,8 @@ char **build_mount_array(const int mount_id, const char *path) { | |||
245 | strstr(mntp.fsname, "firejail.ro.file")) | 236 | strstr(mntp.fsname, "firejail.ro.file")) |
246 | break; | 237 | break; |
247 | 238 | ||
248 | *rv = strdup(path); | 239 | rv[0] = strdup(path); |
249 | if (*rv == NULL) | 240 | if (rv[0] == NULL) |
250 | errExit("strdup"); | 241 | errExit("strdup"); |
251 | cnt++; | 242 | cnt++; |
252 | found = 1; | 243 | found = 1; |