Skip to content

Commit c6bca74

Browse files
committed
rc: libdisk: tighten security around some of the system config files found in /etc ; fix potential resource leak if an error occured when creating ovpn passwd/group/shadow files
1 parent 18f3948 commit c6bca74

File tree

4 files changed

+32
-21
lines changed

4 files changed

+32
-21
lines changed

release/src/router/libdisk/write_smb_conf.c

+1
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ int main(int argc, char *argv[])
748748

749749
use_custom_config("smb.conf", SAMBA_CONF);
750750
run_postconf("smb", SAMBA_CONF);
751+
chmod(SAMBA_CONF, 0644);
751752
}
752753

753754
free_disk_data(&disks_info);

release/src/router/rc/openvpn.c

+19-15
Original file line numberDiff line numberDiff line change
@@ -1962,26 +1962,30 @@ void create_openvpn_passwd()
19621962
fp1=fopen("/etc/shadow.openvpn", "w");
19631963
fp2=fopen("/etc/passwd.openvpn", "w");
19641964
fp3=fopen("/etc/group.openvpn", "w");
1965-
if (!fp1 || !fp2 || !fp3) return;
19661965

1967-
nv = nvp = strdup(nvram_safe_get("vpn_serverx_clientlist"));
1966+
if (fp1 && fp2 && fp3) {
1967+
nv = nvp = strdup(nvram_safe_get("vpn_serverx_clientlist"));
19681968

1969-
if(nv) {
1970-
while ((b = strsep(&nvp, "<")) != NULL) {
1971-
if((vstrsep(b, ">", &username, &passwd)!=2)) continue;
1972-
if(strlen(username)==0||strlen(passwd)==0) continue;
1969+
if(nv) {
1970+
while ((b = strsep(&nvp, "<")) != NULL) {
1971+
if((vstrsep(b, ">", &username, &passwd)!=2)) continue;
1972+
if(strlen(username)==0||strlen(passwd)==0) continue;
19731973

1974-
p = crypt(passwd, salt);
1975-
fprintf(fp1, "%s:%s:0:0:99999:7:0:0:\n", username, p);
1976-
fprintf(fp2, "%s:x:%d:%d:::\n", username, id, id);
1977-
fprintf(fp3, "%s:x:%d:\n", username, id);
1978-
id++;
1974+
p = crypt(passwd, salt);
1975+
fprintf(fp1, "%s:%s:0:0:99999:7:0:0:\n", username, p);
1976+
fprintf(fp2, "%s:x:%d:%d:::\n", username, id, id);
1977+
fprintf(fp3, "%s:x:%d:\n", username, id);
1978+
id++;
1979+
}
1980+
free(nv);
19791981
}
1980-
free(nv);
19811982
}
1982-
fclose(fp1);
1983-
fclose(fp2);
1984-
fclose(fp3);
1983+
if (fp1) fclose(fp1);
1984+
if (fp2) fclose(fp2);
1985+
if (fp3) fclose(fp3);
1986+
chmod("/etc/shadow.openvpn", 0600);
1987+
chmod("/etc/group.openvpn", 0644);
1988+
chmod("/etc/passwd.openvpn", 0644);
19851989
}
19861990

19871991

release/src/router/rc/services.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,7 @@ void start_dnsmasq(void)
935935
fclose(fp);
936936
use_custom_config("hosts", "/etc/hosts");
937937
run_postconf("hosts","/etc/hosts");
938+
chmod("/etc/hosts", 0644);
938939
} else
939940
perror("/etc/hosts");
940941

@@ -1315,12 +1316,13 @@ void start_dnsmasq(void)
13151316

13161317
use_custom_config("dnsmasq.conf","/etc/dnsmasq.conf");
13171318
run_postconf("dnsmasq","/etc/dnsmasq.conf");
1319+
chmod("/etc/dnsmasq.conf", 0644);
13181320

13191321
/* Create resolv.conf with empty nameserver list */
1320-
f_write(dmresolv, NULL, 0, FW_APPEND, 0666);
1322+
f_write(dmresolv, NULL, 0, FW_APPEND, 0644);
13211323

13221324
/* Create resolv.dnsmasq with empty server list */
1323-
f_write(dmservers, NULL, 0, FW_APPEND, 0666);
1325+
f_write(dmservers, NULL, 0, FW_APPEND, 0644);
13241326

13251327
#if (defined(RTCONFIG_TR069) && !defined(RTCONFIG_TR181))
13261328
eval("dnsmasq", "--log-async", "-6", "/sbin/dhcpc_lease");

release/src/router/rc/usb.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -2157,14 +2157,18 @@ void create_custom_passwd(void)
21572157
fprintf(fp, "%s:x:%d:%d:::\n", account_list[i], n, n);
21582158
}
21592159
fclose(fp);
2160+
chmod("/etc/passwd.custom", 0644);
21602161

21612162
/* write /etc/group.custom */
21622163
fp = fopen("/etc/group.custom", "w+");
2163-
for (i=0, n=500; i<acc_num; i++, n++)
2164-
{
2165-
fprintf(fp, "%s:x:%d:\n", account_list[i], n);
2164+
if (fp) {
2165+
for (i=0, n=500; i<acc_num; i++, n++)
2166+
{
2167+
fprintf(fp, "%s:x:%d:\n", account_list[i], n);
2168+
}
2169+
fclose(fp);
2170+
chmod("/etc/group.custom", 0644);
21662171
}
2167-
fclose(fp);
21682172
free_2_dimension_list(&acc_num, &account_list);
21692173
}
21702174

0 commit comments

Comments
 (0)