diff options
Diffstat (limited to 'src/firejail/fs_hostname.c')
-rw-r--r-- | src/firejail/fs_hostname.c | 28 |
1 files changed, 11 insertions, 17 deletions
diff --git a/src/firejail/fs_hostname.c b/src/firejail/fs_hostname.c index 04197eb8f..dcf06fc6f 100644 --- a/src/firejail/fs_hostname.c +++ b/src/firejail/fs_hostname.c | |||
@@ -33,15 +33,7 @@ void fs_hostname(const char *hostname) { | |||
33 | if (arg_debug) | 33 | if (arg_debug) |
34 | printf("Creating a new /etc/hostname file\n"); | 34 | printf("Creating a new /etc/hostname file\n"); |
35 | 35 | ||
36 | FILE *fp = fopen(RUN_HOSTNAME_FILE, "w"); | 36 | create_empty_file_as_root(RUN_HOSTNAME_FILE, S_IRUSR | S_IWRITE | S_IRGRP | S_IROTH); |
37 | if (!fp) { | ||
38 | fprintf(stderr, "Error: cannot create %s\n", RUN_HOSTNAME_FILE); | ||
39 | exit(1); | ||
40 | } | ||
41 | fprintf(fp, "%s\n", hostname); | ||
42 | // mode and owner | ||
43 | SET_PERMS_STREAM(fp, 0, 0, S_IRUSR | S_IWRITE | S_IRGRP | S_IROTH); | ||
44 | fclose(fp); | ||
45 | 37 | ||
46 | // bind-mount the file on top of /etc/hostname | 38 | // bind-mount the file on top of /etc/hostname |
47 | if (mount(RUN_HOSTNAME_FILE, "/etc/hostname", NULL, MS_BIND|MS_REC, NULL) < 0) | 39 | if (mount(RUN_HOSTNAME_FILE, "/etc/hostname", NULL, MS_BIND|MS_REC, NULL) < 0) |
@@ -56,15 +48,12 @@ void fs_hostname(const char *hostname) { | |||
56 | // copy /etc/host into our new file, and modify it on the fly | 48 | // copy /etc/host into our new file, and modify it on the fly |
57 | /* coverity[toctou] */ | 49 | /* coverity[toctou] */ |
58 | FILE *fp1 = fopen("/etc/hosts", "r"); | 50 | FILE *fp1 = fopen("/etc/hosts", "r"); |
59 | if (!fp1) { | 51 | if (!fp1) |
60 | fprintf(stderr, "Error: cannot open /etc/hosts\n"); | 52 | goto errexit; |
61 | exit(1); | 53 | |
62 | } | ||
63 | FILE *fp2 = fopen(RUN_HOSTS_FILE, "w"); | 54 | FILE *fp2 = fopen(RUN_HOSTS_FILE, "w"); |
64 | if (!fp2) { | 55 | if (!fp2) |
65 | fprintf(stderr, "Error: cannot create %s\n", RUN_HOSTS_FILE); | 56 | goto errexit; |
66 | exit(1); | ||
67 | } | ||
68 | 57 | ||
69 | char buf[4096]; | 58 | char buf[4096]; |
70 | int done = 0; | 59 | int done = 0; |
@@ -92,6 +81,11 @@ void fs_hostname(const char *hostname) { | |||
92 | errExit("mount bind /etc/hosts"); | 81 | errExit("mount bind /etc/hosts"); |
93 | fs_logger("create /etc/hosts"); | 82 | fs_logger("create /etc/hosts"); |
94 | } | 83 | } |
84 | return; | ||
85 | |||
86 | errexit: | ||
87 | fprintf(stderr, "Error: cannot create hostname file\n"); | ||
88 | exit(1); | ||
95 | } | 89 | } |
96 | 90 | ||
97 | void fs_resolvconf(void) { | 91 | void fs_resolvconf(void) { |