1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
/*
* Copyright (C) 2014-2018 Firejail Authors
*
* This file is part of firejail project
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "firejail.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAXBUF 4098
typedef struct fs_msg_t {
struct fs_msg_t *next;
char *msg;
} FsMsg;
static FsMsg *head = NULL;
static inline FsMsg *newmsg(void) {
FsMsg *ptr = malloc(sizeof(FsMsg));
if (!ptr)
errExit("malloc");
memset(ptr, 0, sizeof(FsMsg));
return ptr;
}
static inline void insertmsg(FsMsg *ptr) {
static FsMsg *last = NULL;
if (head == NULL) {
head = ptr;
last = ptr;
return;
}
assert(last);
last->next = ptr;
last = ptr;
}
void fs_logger(const char *msg) {
FsMsg *ptr = newmsg();
ptr->msg = strdup(msg);
if (!ptr->msg)
errExit("strdup");
insertmsg(ptr);
}
void fs_logger2(const char *msg1, const char *msg2) {
FsMsg *ptr = newmsg();
char *str;
if (asprintf(&str, "%s %s", msg1, msg2) == -1)
errExit("asprintf");
ptr->msg = str;
insertmsg(ptr);
}
void fs_logger2int(const char *msg1, int d) {
FsMsg *ptr = newmsg();
char *str;
if (asprintf(&str, "%s %d", msg1, d) == -1)
errExit("asprintf");
ptr->msg = str;
insertmsg(ptr);
}
void fs_logger3(const char *msg1, const char *msg2, const char *msg3) {
FsMsg *ptr = newmsg();
char *str;
if (asprintf(&str, "%s %s %s", msg1, msg2, msg3) == -1)
errExit("asprintf");
ptr->msg = str;
insertmsg(ptr);
}
void fs_logger_print(void) {
if (!head)
return;
FILE *fp = fopen(RUN_FSLOGGER_FILE, "a");
if (!fp) {
perror("fopen");
return;
}
SET_PERMS_STREAM_NOERR(fp, getuid(), getgid(), 0644);
FsMsg *ptr = head;
while (ptr) {
fprintf(fp, "%s\n", ptr->msg);
FsMsg *next = ptr->next;
// remove message
free(ptr->msg);
free(ptr);
ptr = next;
}
head = NULL;
fclose(fp);
}
void fs_logger_change_owner(void) {
if (chown(RUN_FSLOGGER_FILE, 0, 0) == -1)
errExit("chown");
}
void fs_logger_print_log(pid_t pid) {
EUID_ASSERT();
// if the pid is that of a firejail process, use the pid of the first child process
EUID_ROOT();
char *comm = pid_proc_comm(pid);
EUID_USER();
if (comm) {
if (strcmp(comm, "firejail") == 0) {
pid_t child;
if (find_child(pid, &child) == 0) {
pid = child;
}
}
free(comm);
}
// check privileges for non-root users
uid_t uid = getuid();
if (uid != 0) {
uid_t sandbox_uid = pid_get_uid(pid);
if (uid != sandbox_uid) {
fprintf(stderr, "Error: permission denied\n");
exit(1);
}
}
// print RUN_FSLOGGER_FILE
char *fname;
if (asprintf(&fname, "/proc/%d/root%s", pid, RUN_FSLOGGER_FILE) == -1)
errExit("asprintf");
EUID_ROOT();
struct stat s;
if (stat(fname, &s) == -1) {
fprintf(stderr, "Error: Cannot access filesystem log\n");
exit(1);
}
/* coverity[toctou] */
FILE *fp = fopen(fname, "r");
if (!fp) {
fprintf(stderr, "Error: Cannot open filesystem log\n");
exit(1);
}
char buf[MAXBUF];
while (fgets(buf, MAXBUF, fp))
printf("%s", buf);
fclose(fp);
free(fname);
exit(0);
}
|