diff options
Diffstat (limited to 'src/firejail/fs_hostname.c')
-rw-r--r-- | src/firejail/fs_hostname.c | 47 |
1 files changed, 16 insertions, 31 deletions
diff --git a/src/firejail/fs_hostname.c b/src/firejail/fs_hostname.c index 2d9ad6fa7..94251df57 100644 --- a/src/firejail/fs_hostname.c +++ b/src/firejail/fs_hostname.c | |||
@@ -33,49 +33,40 @@ void fs_hostname(const char *hostname) { | |||
33 | if (stat("/etc/hostname", &s) == 0) { | 33 | if (stat("/etc/hostname", &s) == 0) { |
34 | if (arg_debug) | 34 | if (arg_debug) |
35 | printf("Creating a new /etc/hostname file\n"); | 35 | printf("Creating a new /etc/hostname file\n"); |
36 | char *fhost; | 36 | |
37 | if (asprintf(&fhost, "%s/hostname", MNT_DIR) == -1) | 37 | FILE *fp = fopen(HOSTNAME_FILE, "w"); |
38 | errExit("asprintf"); | ||
39 | FILE *fp = fopen(fhost, "w"); | ||
40 | if (!fp) { | 38 | if (!fp) { |
41 | fprintf(stderr, "Error: cannot create %s\n", fhost); | 39 | fprintf(stderr, "Error: cannot create %s\n", HOSTNAME_FILE); |
42 | free(fhost); | ||
43 | exit(1); | 40 | exit(1); |
44 | } | 41 | } |
45 | fprintf(fp, "%s\n", hostname); | 42 | fprintf(fp, "%s\n", hostname); |
46 | fclose(fp); | 43 | fclose(fp); |
47 | 44 | ||
48 | // mode and owner | 45 | // mode and owner |
49 | if (chown(fhost, 0, 0) < 0) | 46 | if (chown(HOSTNAME_FILE, 0, 0) < 0) |
50 | errExit("chown"); | 47 | errExit("chown"); |
51 | if (chmod(fhost, S_IRUSR | S_IWRITE | S_IRGRP | S_IROTH ) < 0) | 48 | if (chmod(HOSTNAME_FILE, S_IRUSR | S_IWRITE | S_IRGRP | S_IROTH ) < 0) |
52 | errExit("chmod"); | 49 | errExit("chmod"); |
53 | 50 | ||
54 | // bind-mount the file on top of /etc/hostname | 51 | // bind-mount the file on top of /etc/hostname |
55 | if (mount(fhost, "/etc/hostname", NULL, MS_BIND|MS_REC, NULL) < 0) | 52 | if (mount(HOSTNAME_FILE, "/etc/hostname", NULL, MS_BIND|MS_REC, NULL) < 0) |
56 | errExit("mount bind /etc/hostname"); | 53 | errExit("mount bind /etc/hostname"); |
57 | free(fhost); | ||
58 | } | 54 | } |
59 | 55 | ||
60 | // create a new /etc/hosts | 56 | // create a new /etc/hosts |
61 | if (stat("/etc/hosts", &s) == 0) { | 57 | if (stat("/etc/hosts", &s) == 0) { |
62 | if (arg_debug) | 58 | if (arg_debug) |
63 | printf("Creating a new /etc/hosts file\n"); | 59 | printf("Creating a new /etc/hosts file\n"); |
64 | char *fhost; | ||
65 | if (asprintf(&fhost, "%s/hosts", MNT_DIR) == -1) | ||
66 | errExit("asprintf"); | ||
67 | // copy /etc/host into our new file, and modify it on the fly | 60 | // copy /etc/host into our new file, and modify it on the fly |
68 | /* coverity[toctou] */ | 61 | /* coverity[toctou] */ |
69 | FILE *fp1 = fopen("/etc/hosts", "r"); | 62 | FILE *fp1 = fopen("/etc/hosts", "r"); |
70 | if (!fp1) { | 63 | if (!fp1) { |
71 | fprintf(stderr, "Error: cannot open /etc/hosts\n"); | 64 | fprintf(stderr, "Error: cannot open /etc/hosts\n"); |
72 | free(fhost); | ||
73 | exit(1); | 65 | exit(1); |
74 | } | 66 | } |
75 | FILE *fp2 = fopen(fhost, "w"); | 67 | FILE *fp2 = fopen(HOSTNAME_FILE, "w"); |
76 | if (!fp2) { | 68 | if (!fp2) { |
77 | fprintf(stderr, "Error: cannot create %s\n", fhost); | 69 | fprintf(stderr, "Error: cannot create %s\n", HOSTNAME_FILE); |
78 | free(fhost); | ||
79 | exit(1); | 70 | exit(1); |
80 | } | 71 | } |
81 | 72 | ||
@@ -96,15 +87,14 @@ void fs_hostname(const char *hostname) { | |||
96 | fclose(fp2); | 87 | fclose(fp2); |
97 | 88 | ||
98 | // mode and owner | 89 | // mode and owner |
99 | if (chown(fhost, 0, 0) < 0) | 90 | if (chown(HOSTNAME_FILE, 0, 0) < 0) |
100 | errExit("chown"); | 91 | errExit("chown"); |
101 | if (chmod(fhost, S_IRUSR | S_IWRITE | S_IRGRP | S_IROTH ) < 0) | 92 | if (chmod(HOSTNAME_FILE, S_IRUSR | S_IWRITE | S_IRGRP | S_IROTH ) < 0) |
102 | errExit("chmod"); | 93 | errExit("chmod"); |
103 | 94 | ||
104 | // bind-mount the file on top of /etc/hostname | 95 | // bind-mount the file on top of /etc/hostname |
105 | if (mount(fhost, "/etc/hosts", NULL, MS_BIND|MS_REC, NULL) < 0) | 96 | if (mount(HOSTNAME_FILE, "/etc/hosts", NULL, MS_BIND|MS_REC, NULL) < 0) |
106 | errExit("mount bind /etc/hosts"); | 97 | errExit("mount bind /etc/hosts"); |
107 | free(fhost); | ||
108 | } | 98 | } |
109 | } | 99 | } |
110 | 100 | ||
@@ -119,13 +109,9 @@ void fs_resolvconf(void) { | |||
119 | if (stat("/etc/resolv.conf", &s) == 0) { | 109 | if (stat("/etc/resolv.conf", &s) == 0) { |
120 | if (arg_debug) | 110 | if (arg_debug) |
121 | printf("Creating a new /etc/resolv.conf file\n"); | 111 | printf("Creating a new /etc/resolv.conf file\n"); |
122 | char *fname; | 112 | FILE *fp = fopen(RESOLVCONF_FILE, "w"); |
123 | if (asprintf(&fname, "%s/resolv.conf", MNT_DIR) == -1) | ||
124 | errExit("asprintf"); | ||
125 | FILE *fp = fopen(fname, "w"); | ||
126 | if (!fp) { | 113 | if (!fp) { |
127 | fprintf(stderr, "Error: cannot create %s\n", fname); | 114 | fprintf(stderr, "Error: cannot create %s\n", RESOLVCONF_FILE); |
128 | free(fname); | ||
129 | exit(1); | 115 | exit(1); |
130 | } | 116 | } |
131 | 117 | ||
@@ -138,15 +124,14 @@ void fs_resolvconf(void) { | |||
138 | fclose(fp); | 124 | fclose(fp); |
139 | 125 | ||
140 | // mode and owner | 126 | // mode and owner |
141 | if (chown(fname, 0, 0) < 0) | 127 | if (chown(RESOLVCONF_FILE, 0, 0) < 0) |
142 | errExit("chown"); | 128 | errExit("chown"); |
143 | if (chmod(fname, S_IRUSR | S_IWRITE | S_IRGRP | S_IROTH ) < 0) | 129 | if (chmod(RESOLVCONF_FILE, S_IRUSR | S_IWRITE | S_IRGRP | S_IROTH ) < 0) |
144 | errExit("chmod"); | 130 | errExit("chmod"); |
145 | 131 | ||
146 | // bind-mount the file on top of /etc/hostname | 132 | // bind-mount the file on top of /etc/hostname |
147 | if (mount(fname, "/etc/resolv.conf", NULL, MS_BIND|MS_REC, NULL) < 0) | 133 | if (mount(RESOLVCONF_FILE, "/etc/resolv.conf", NULL, MS_BIND|MS_REC, NULL) < 0) |
148 | errExit("mount bind /etc/resolv.conf"); | 134 | errExit("mount bind /etc/resolv.conf"); |
149 | free(fname); | ||
150 | } | 135 | } |
151 | else { | 136 | else { |
152 | fprintf(stderr, "Error: cannot set DNS servers, /etc/resolv.conf file is missing\n"); | 137 | fprintf(stderr, "Error: cannot set DNS servers, /etc/resolv.conf file is missing\n"); |