Skip to content

Commit ee73708

Browse files
committed
modif: improve error messages in mountinfo.c
Currently when a line from /proc/self/mountinfo is too long, firejail exits with an unclear error message: Error: cannot read /proc/self/mountinfo Relates to #6450.
1 parent 62f477d commit ee73708

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/firejail/mountinfo.c

+21-8
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ static void parse_line(char *line, MountData *output) {
7979
// output.dir: /home/netblue/.cache
8080
// output.fstype: tmpfs
8181

82+
size_t linelen = strlen(line);
83+
if (linelen >= MAX_BUF - 1) {
84+
fprintf(stderr, "Error: /proc/self/mountinfo line is too long and may be truncated (%zu >= %d): %s\n",
85+
linelen, MAX_BUF - 1, line);
86+
exit(1);
87+
}
88+
89+
char *orig_line = strdup(line);
8290
char *ptr = strtok(line, " ");
8391
if (!ptr)
8492
goto errexit;
@@ -107,17 +115,22 @@ static void parse_line(char *line, MountData *output) {
107115
if (output->mountid < 0 ||
108116
output->fsname == NULL ||
109117
output->dir == NULL ||
110-
output->fstype == NULL)
118+
output->fstype == NULL) {
119+
fprintf(stderr, "Error: invalid MountData\n");
111120
goto errexit;
121+
}
112122

113123
// restore empty spaces
114124
unmangle_path(output->fsname);
115125
unmangle_path(output->dir);
116126

127+
free(orig_line);
117128
return;
118129

119130
errexit:
120-
fprintf(stderr, "Error: cannot read /proc/self/mountinfo\n");
131+
fprintf(stderr, "Error: cannot parse line from /proc/self/mountinfo: %s", orig_line);
132+
fprintf(stderr, "Error: MountData: mountid=%d fsname=%s dir=%s fstype=%s\n",
133+
output->mountid, output->fsname, output->dir, output->fstype);
121134
exit(1);
122135
}
123136

@@ -127,7 +140,7 @@ MountData *get_last_mount(void) {
127140
FILE *fp = fopen("/proc/self/mountinfo", "re");
128141
if (!fp) {
129142
fprintf(stderr, "Error: cannot read /proc/self/mountinfo\n");
130-
exit(1);
143+
errExit("fopen");
131144
}
132145

133146
mbuf[0] = '\0';
@@ -158,8 +171,8 @@ static int get_mount_id_from_handle(int fd) {
158171
int rv = -1;
159172
int tmp;
160173
if (name_to_handle_at(-1, proc, fh, &tmp, AT_SYMLINK_FOLLOW) != -1) {
161-
fprintf(stderr, "Error: unexpected result from name_to_handle_at\n");
162-
exit(1);
174+
fprintf(stderr, "Error: unexpected result from name_to_handle_at for fd %d\n", fd);
175+
errExit("name_to_handle_at");
163176
}
164177
if (errno == EOVERFLOW && fh->handle_bytes)
165178
rv = tmp;
@@ -184,8 +197,8 @@ static int get_mount_id_from_fdinfo(int fd) {
184197

185198
FILE *fp = fopen(proc, "re");
186199
if (!fp) {
187-
fprintf(stderr, "Error: cannot read proc file\n");
188-
exit(1);
200+
fprintf(stderr, "Error: cannot read %s\n", proc);
201+
errExit("fopen");
189202
}
190203

191204
if (called_as_root == 0)
@@ -218,7 +231,7 @@ char **build_mount_array(const int mountid, const char *path) {
218231
FILE *fp = fopen("/proc/self/mountinfo", "re");
219232
if (!fp) {
220233
fprintf(stderr, "Error: cannot read /proc/self/mountinfo\n");
221-
exit(1);
234+
errExit("fopen");
222235
}
223236

224237
// try to find line with mount id

0 commit comments

Comments
 (0)