Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unitialized access of err and erroneous error message. #41

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions examples/chroot_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ int main(int argc, char *const argv[])
int i;

if (argc < 3) {
printf("Invalid arguments\n");
printf("Usage: %s NEWROOT COMMAND [ARG...]\n", argv[0]);
fprintf(stderr, "usage: %s NEWROOT COMMAND [ARG...]\n", argv[0]);
return -1;
}

Expand Down Expand Up @@ -89,8 +88,8 @@ int main(int argc, char *const argv[])
volume_len = strlen(current_path) + strlen(volume_tail) + 1;
volume = malloc(volume_len);
if (volume == NULL) {
errno = -err;
perror("Error allocating memory for volume string");
return -1;
}

snprintf(volume, volume_len, "%s%s", current_path, volume_tail);
Expand Down
5 changes: 2 additions & 3 deletions examples/sev-attest.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ int main(int argc, char *const argv[])
int i;

if (argc != 3) {
printf("Invalid arguments\n");
printf("Usage: %s DISK_IMAGE ATTESTATION_URL\n", argv[0]);
fprintf(stderr, "usage: %s DISK_IMAGE ATTESTATION_URL\n", argv[0]);
return -1;
}

Expand Down Expand Up @@ -83,8 +82,8 @@ int main(int argc, char *const argv[])
volume_len = strlen(current_path) + strlen(volume_tail) + 1;
volume = malloc(volume_len);
if (volume == NULL) {
errno = -err;
perror("Error allocating memory for volume string");
return -1;
}

// Map port 18000 in the host to 8000 in the guest.
Expand Down
5 changes: 2 additions & 3 deletions examples/sev-noattest.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ int main(int argc, char *const argv[])
int i;

if (argc < 4) {
printf("Invalid arguments\n");
printf("Usage: %s DISK_IMAGE PASSPHRASE COMMAND [ARG...]\n", argv[0]);
fprintf(stderr, "usage: %s DISK_IMAGE PASSPHRASE COMMAND [ARG...]\n", argv[0]);
return -1;
}

Expand Down Expand Up @@ -89,8 +88,8 @@ int main(int argc, char *const argv[])
volume_len = strlen(current_path) + strlen(volume_tail) + 1;
volume = malloc(volume_len);
if (volume == NULL) {
errno = -err;
perror("Error allocating memory for volume string");
return -1;
}

// Map port 18000 in the host to 8000 in the guest.
Expand Down
88 changes: 42 additions & 46 deletions init/init.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <limits.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
Expand Down Expand Up @@ -29,7 +30,7 @@ void set_rlimits(const char *rlimits)

lim_id = strtoull(item, &item, 10);
if (lim_id == ULLONG_MAX) {
printf("Invalid rlimit ID\n");
fprintf(stderr, "Invalid rlimit ID\n");
break;
}

Expand All @@ -41,7 +42,7 @@ void set_rlimits(const char *rlimits)
rlim.rlim_cur = lim_cur;
rlim.rlim_max = lim_max;
if (setrlimit(lim_id, &rlim) != 0) {
printf("Error setting rlimit for ID=%lld\n", lim_id);
fprintf(stderr, "Can't set rlimit for ID=%llu\n", lim_id);
}

if (*item != '\0') {
Expand Down Expand Up @@ -69,84 +70,79 @@ int main(int argc, char **argv)
if (passp) {
printf("Unlocking LUKS root filesystem\n");

if (mount("proc", "/proc", "proc",
MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
perror("mount(/proc)");
exit(-1);
}
if (mount("proc", "/proc", "proc",
MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
perror("mount(/proc)");
exit(-1);
}

pipe(pipefd);
pipe(pipefd);

pid = fork();
if (pid == 0) {
pid = fork();
if (pid == -1) {
err(-1, "fork");
} else if (pid == 0) {
close(pipefd[1]);
dup2(pipefd[0], 0);
close(pipefd[0]);
dup2(pipefd[0], 0);
close(pipefd[0]);

if (execl("/sbin/cryptsetup", "cryptsetup", "open", "/dev/vda", "luksroot", "-", NULL) < 0) {
perror("execl");
exit(-1);
if (execl("/sbin/cryptsetup", "cryptsetup", "open", "/dev/vda",
"luksroot", "-", NULL) < 0) {
err(-1, "execl");
}
} else {
if (write(pipefd[1], passp, strnlen(passp, 128)) < 0) {
warn("write"); // XXX - ignores short count
}
} else {
write(pipefd[1], passp, strnlen(passp, 128));
close(pipefd[1]);
waitpid(pid, &wstatus, 0);
}
close(pipefd[1]);
waitpid(pid, &wstatus, 0);
}

printf("Mounting LUKS root filesystem\n");

if (mount("/dev/mapper/luksroot", "/luksroot", "ext4", 0, NULL) < 0) {
perror("mount(/luksroot)");
exit(-1);
}
if (mount("/dev/mapper/luksroot", "/luksroot", "ext4", 0, NULL) < 0) {
err(-1, "mount(/luksroot)");
}

chdir("/luksroot");
chdir("/luksroot");

if (mount(".", "/", NULL, MS_MOVE, NULL)) {
perror("remount root");
exit(-1);
err(-1, "remount root");
}
chroot(".");
}

if (mount("proc", "/proc", "proc",
MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
perror("mount(/proc)");
exit(-1);
MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
err(-1, "mount(/proc)");
}

if (mount("sysfs", "/sys", "sysfs",
MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
perror("mount(/sys)");
exit(-1);
MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
err(-1, "mount(/sys)");
}

if (mount("cgroup2", "/sys/fs/cgroup", "cgroup2",
MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
perror("mount(/sys/fs/cgroup)");
exit(-1);
MS_NODEV | MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
err(-1, "mount(/sys/fs/cgroup)");
}

if (mkdir("/dev/pts", 0755) < 0 && errno != EEXIST) {
perror("mkdir(/dev/pts)");
exit(-1);
err(-1, "mkdir(/dev/pts)");
}

if (mount("devpts", "/dev/pts", "devpts",
MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
perror("mount(/dev/pts)");
exit(-1);
MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
err(-1, "mount(/dev/pts)");
}

if (mkdir("/dev/shm", 0755) < 0 && errno != EEXIST) {
perror("mkdir(/dev/shm)");
exit(-1);
err(-1, "mkdir(/dev/shm)");
}

if (mount("tmpfs", "/dev/shm", "tmpfs",
MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
perror("mount(/dev/shm)");
exit(-1);
MS_NOEXEC | MS_NOSUID | MS_RELATIME, NULL) < 0) {
err(-1, "mount(/dev/shm)");
}

/* May fail if already exists and that's fine. */
Expand Down