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
|
/*
* Copyright (C) 2014-2020 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>
void check_output(int argc, char **argv) {
EUID_ASSERT();
int i;
int outindex = 0;
int enable_stderr = 0;
for (i = 1; i < argc; i++) {
if (strncmp(argv[i], "--", 2) != 0) {
return;
}
if (strcmp(argv[i], "--") == 0) {
return;
}
if (strncmp(argv[i], "--output=", 9) == 0) {
outindex = i;
break;
}
if (strncmp(argv[i], "--output-stderr=", 16) == 0) {
outindex = i;
enable_stderr = 1;
break;
}
}
if (!outindex)
return;
// check filename
drop_privs(0);
char *outfile = argv[outindex];
outfile += (enable_stderr)? 16:9;
invalid_filename(outfile, 0); // no globbing
// do not accept directories, links, and files with ".."
if (strstr(outfile, "..") || is_link(outfile) || is_dir(outfile)) {
fprintf(stderr, "Error: invalid output file. Links, directories and files with \"..\" are not allowed.\n");
exit(1);
}
struct stat s;
if (stat(outfile, &s) == 0) {
// check permissions
if (s.st_uid != getuid() || s.st_gid != getgid()) {
fprintf(stderr, "Error: the output file needs to be owned by the current user.\n");
exit(1);
}
// check hard links
if (s.st_nlink != 1) {
fprintf(stderr, "Error: no hard links allowed.\n");
exit(1);
}
}
int pipefd[2];
if (pipe(pipefd) == -1) {
errExit("pipe");
}
pid_t pid = fork();
if (pid == -1) {
errExit("fork");
} else if (pid == 0) {
/* child */
if (dup2(pipefd[0], STDIN_FILENO) == -1) {
errExit("dup2");
}
close(pipefd[1]);
if (pipefd[0] != STDIN_FILENO) {
close(pipefd[0]);
}
char *args[3];
args[0] = LIBDIR "/firejail/ftee";
args[1] = outfile;
args[2] = NULL;
execv(args[0], args);
perror("execvp");
exit(1);
}
/* parent */
if (dup2(pipefd[1], STDOUT_FILENO) == -1) {
errExit("dup2");
}
if (enable_stderr && dup2(STDOUT_FILENO, STDERR_FILENO) == -1) {
errExit("dup2");
}
close(pipefd[0]);
if (pipefd[1] != STDOUT_FILENO) {
close(pipefd[1]);
}
char **args = calloc(argc + 1, sizeof(char *));
if (!args) {
errExit("calloc");
}
bool found_separator = false;
/* copy argv into args, but drop --output(-stderr) arguments */
int j;
for (i = 0, j = 0; i < argc; i++) {
if (!found_separator && i > 0) {
if (strncmp(argv[i], "--output=", 9) == 0) {
continue;
}
if (strncmp(argv[i], "--output-stderr=", 16) == 0) {
continue;
}
if (strncmp(argv[i], "--", 2) != 0 || strcmp(argv[i], "--") == 0) {
found_separator = true;
}
}
args[j++] = argv[i];
}
execvp(args[0], args);
perror("execvp");
exit(1);
}
|