makefs: strdup arguments to mkfs

Don't pass values from argv[] directly to child process forked using
safe_fork, because it clears argv[]. strdup them first.
This commit is contained in:
Oliver Giles 2020-02-13 08:55:57 +02:00 committed by Yu Watanabe
parent 1d6cfd25a2
commit c315b79fb4

View file

@ -41,8 +41,7 @@ static int makefs(const char *type, const char *device) {
} }
static int run(int argc, char *argv[]) { static int run(int argc, char *argv[]) {
const char *device, *type; _cleanup_free_ char *device = NULL, *type = NULL, *detected = NULL;
_cleanup_free_ char *detected = NULL;
struct stat st; struct stat st;
int r; int r;
@ -52,8 +51,14 @@ static int run(int argc, char *argv[]) {
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"This program expects two arguments."); "This program expects two arguments.");
type = argv[1]; /* type and device must be copied because makefs calls safe_fork, which clears argv[] */
device = argv[2]; type = strdup(argv[1]);
if (!type)
return -ENOMEM;
device = strdup(argv[2]);
if (!device)
return -ENOMEM;
if (stat(device, &st) < 0) if (stat(device, &st) < 0)
return log_error_errno(errno, "Failed to stat \"%s\": %m", device); return log_error_errno(errno, "Failed to stat \"%s\": %m", device);