Skip to content

Commit 20673c3

Browse files
authored
Merge pull request #1236 from kernelkit/recopy
bin: copy: Fix various resource leaks introduced by refactor
2 parents 0328700 + e718644 commit 20673c3

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

board/common/rootfs/usr/libexec/infix/mksshkey

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ echo -e "$PUBLIC" >> "$TMP"
1818
echo -e "-----END RSA PUBLIC KEY-----" >> "$TMP"
1919

2020
ssh-keygen -i -m PKCS8 -f "$TMP" > "$DIR/$NAME.pub"
21+
rm "$TMP"
2122
chmod 0600 "$DIR/$NAME.pub"
2223
chmod 0600 "$DIR/$NAME"
2324
chown sshd:sshd "$DIR/$NAME.pub"

src/bin/copy.c

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,30 @@ static bool is_uri(const char *str)
155155
return strstr(str, "://") != NULL;
156156
}
157157

158+
static char *mktmp(void)
159+
{
160+
mode_t oldmask;
161+
char *path;
162+
int fd;
163+
164+
path = strdup("/tmp/copy-XXXXXX");
165+
if (!path)
166+
goto err;
167+
168+
oldmask = umask(0077);
169+
fd = mkstemp(path);
170+
umask(oldmask);
171+
172+
if (fd < 0)
173+
goto err;
174+
175+
close(fd);
176+
return path;
177+
err:
178+
free(path);
179+
return NULL;
180+
}
181+
158182
static void rmtmp(const char *path)
159183
{
160184
if (remove(path)) {
@@ -338,7 +362,7 @@ static int curl(char *op, const char *path, const char *uri)
338362
char *argv[] = {
339363
"curl", "-L", op, NULL, NULL, NULL, NULL, NULL,
340364
};
341-
int err;
365+
int err = 1;
342366

343367
argv[3] = strdup(path);
344368
argv[4] = strdup(uri);
@@ -390,7 +414,7 @@ static int cp(const char *srcpath, const char *dstpath)
390414
char *argv[] = {
391415
"cp", NULL, NULL, NULL,
392416
};
393-
int err;
417+
int err = 1;
394418

395419
argv[1] = strdup(srcpath);
396420
argv[2] = strdup(dstpath);
@@ -445,7 +469,7 @@ static int resolve_src(const char **src, const struct infix_ds **ds, char **path
445469
*src = infix_ds(*src, ds);
446470

447471
if (*ds || is_uri(*src)) {
448-
*path = tempnam(NULL, NULL);
472+
*path = mktmp();
449473
if (!*path)
450474
return 1;
451475

@@ -477,7 +501,7 @@ static int resolve_dst(const char **dst, const struct infix_ds **ds, char **path
477501
if (!(*ds)->path)
478502
return 0;
479503

480-
*path = (*ds)->path;
504+
*path = strdup((*ds)->path);
481505
} else if (is_uri(*dst)) {
482506
return 0;
483507
} else {
@@ -499,8 +523,8 @@ static int resolve_dst(const char **dst, const struct infix_ds **ds, char **path
499523

500524
static int copy(const char *src, const char *dst)
501525
{
526+
char *srcpath = NULL, *dstpath = NULL;
502527
const struct infix_ds *srcds, *dstds;
503-
char *srcpath, *dstpath;
504528
bool rmsrc = false;
505529
mode_t oldmask;
506530
int err = 1;
@@ -534,6 +558,9 @@ static int copy(const char *src, const char *dst)
534558
if (rmsrc)
535559
rmtmp(srcpath);
536560

561+
free(dstpath);
562+
free(srcpath);
563+
537564
sync();
538565
umask(oldmask);
539566
return err;

src/bin/erase.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static int sanitize;
1616
static int do_erase(const char *name)
1717
{
1818
char *path;
19-
int rc;
19+
int rc = 0;
2020

2121
path = cfg_adjust(name, NULL, sanitize);
2222
if (!path) {
@@ -25,10 +25,8 @@ static int do_erase(const char *name)
2525
goto out;
2626
}
2727

28-
if (!yorn("Remove %s, are you sure?", path)) {
29-
rc = 0;
28+
if (!yorn("Remove %s, are you sure?", path))
3029
goto out;
31-
}
3230

3331
if (remove(path)) {
3432
fprintf(stderr, ERRMSG "failed removing %s: %s\n", path, strerror(errno));

src/bin/util.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,26 @@ char *cfg_adjust(const char *path, const char *template, bool sanitize)
138138
strchr(basename, '.') ? "" : ".cfg") < 0)
139139
goto err;
140140

141-
/* If file exists, resolve symlinks and verify still in whitelist */
142-
if (sanitize && !access(expanded, F_OK)) {
141+
if (sanitize) {
143142
resolved = realpath(expanded, NULL);
144-
if (!resolved || !path_allowed(resolved))
143+
if (!resolved) {
144+
if (errno == ENOENT)
145+
goto out;
146+
else
147+
goto err;
148+
}
149+
150+
/* File exists, make sure that the resolved symlink
151+
* still matches the whitelist.
152+
*/
153+
if (!path_allowed(resolved))
145154
goto err;
146155

147156
free(expanded);
148157
expanded = resolved;
149158
}
150159

160+
out:
151161
return expanded;
152162

153163
err:

0 commit comments

Comments
 (0)