aboutsummaryrefslogtreecommitdiffstats
path: root/src/firejail/output.c
blob: 91fe7f16400abd0381e32b14504c2e5278cff649 (plain) (blame)
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
/*
 * Copyright (C) 2014-2016 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;
	char *outfile = NULL;

	int found = 0;
	for (i = 1; i < argc; i++) {
		if (strncmp(argv[i], "--output=", 9) == 0) {
			found = 1;
			invalid_filename(argv[i] + 9);
			outfile = argv[i] + 9;

			// 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);
				}
			}

			/* coverity[toctou] */
			FILE *fp = fopen(outfile, "a");
			if (!fp) {
				fprintf(stderr, "Error: cannot open output file %s\n", outfile);
				exit(1);
			}
			fclose(fp);
			break;
		}
	}
	if (!found)
		return;


	// build the new command line
	int len = 0;
	for (i = 0; i < argc; i++) {
		len += strlen(argv[i]) + 1; // + ' '
	}
	len += 100 + strlen(LIBDIR) + strlen(outfile); // tee command
	
	char *cmd = malloc(len + 1); // + '\0'
	if (!cmd)
		errExit("malloc");
	
	char *ptr = cmd;
	for (i = 0; i < argc; i++) {
		if (strncmp(argv[i], "--output=", 9) == 0)
			continue;
		ptr += sprintf(ptr, "%s ", argv[i]);
	}
	sprintf(ptr, "2>&1 | %s/firejail/ftee %s", LIBDIR, outfile);

	// run command
	drop_privs(0);
	char *a[4];
	a[0] = "/bin/bash";
	a[1] = "-c";
	a[2] = cmd;
	a[3] = NULL;

	execvp(a[0], a); 

	perror("execvp");
	exit(1);
}