Signed-off-by: John Crispin <blogic@openwrt.org>
mkdir("/tmp/run", 0777);
mkdir("/tmp/lock", 0777);
mkdir("/tmp/state", 0777);
mkdir("/tmp/run", 0777);
mkdir("/tmp/lock", 0777);
mkdir("/tmp/state", 0777);
- symlink("/tmp", "/var");
+ if (symlink("/tmp", "/var"))
+ ERROR("failed to symlink /tmp -> /var\n");
n_patterns = 1;
find_devs(true);
find_devs(false);
n_patterns = 1;
find_devs(true);
find_devs(false);
- chdir("/");
-
- return 0;
- fscanf(fp, "%d", &lvl);
+ if (fscanf(fp, "%d", &lvl) == EOF)
+ ERROR("failed to read debug level\n");
fclose(fp);
unlink("/tmp/debug_level");
fclose(fp);
unlink("/tmp/debug_level");
- chdir("/dev");
- fd = open( dev, O_RDWR);
- chdir("/");
+ if (chdir("/dev"))
+ ERROR("failed to change dir to /dev\n");
+ fd = open(dev, O_RDWR);
+ if (chdir("/"))
+ ERROR("failed to change dir to /\n");
int res;
res = dev_open(dev);
int res;
res = dev_open(dev);
sysfs = 1;
break;
case 'n':
sysfs = 1;
break;
case 'n':
- sethostname(optarg, strlen(optarg));
+ if (sethostname(optarg, strlen(optarg)))
+ ERROR("failed to sethostname: %s\n", strerror(errno));
- asprintf(&mpoint, "%s/old", path);
+ if (asprintf(&mpoint, "%s/old", path) < 0) {
+ ERROR("failed to alloc pivot path: %s\n", strerror(errno));
+ return -1;
+ }
mkdir_p(mpoint, 0755);
if (pivot_root(path, mpoint) == -1) {
ERROR("pivot_root failed:%s\n", strerror(errno));
mkdir_p(mpoint, 0755);
if (pivot_root(path, mpoint) == -1) {
ERROR("pivot_root failed:%s\n", strerror(errno));
char *dir = get_current_dir_name();
uloop_init();
char *dir = get_current_dir_name();
uloop_init();
+ if (chdir(path)) {
+ ERROR("failed to chdir() into the jail\n");
+ return;
+ }
namespace_process.pid = clone(spawn_child,
child_stack + STACK_SIZE,
CLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWNS | SIGCHLD, argv);
if (namespace_process.pid != -1) {
namespace_process.pid = clone(spawn_child,
child_stack + STACK_SIZE,
CLONE_NEWUTS | CLONE_NEWPID | CLONE_NEWNS | SIGCHLD, argv);
if (namespace_process.pid != -1) {
+ if (chdir(dir))
+ ERROR("failed to chdir() out of the jail\n");
free(dir);
uloop_process_add(&namespace_process);
uloop_run();
free(dir);
uloop_process_add(&namespace_process);
uloop_run();
ERROR("Failed to open %s\n", loadpath);
exit(-1);
}
ERROR("Failed to open %s\n", loadpath);
exit(-1);
}
+ if (write(load, "1", 1) == -1) {
+ ERROR("Failed to write to %s\n", loadpath);
+ exit(-1);
+ }
close(load);
snprintf(syspath, sizeof(syspath), "/sys/%s/data", dev);
close(load);
snprintf(syspath, sizeof(syspath), "/sys/%s/data", dev);
+ if (write(fw, buf, len) == -1) {
+ ERROR("failed to write firmware file %s/%s to %s\n", dir, file, dev);
+ break;
+ }
close(fw);
load = open(loadpath, O_WRONLY);
close(fw);
load = open(loadpath, O_WRONLY);
+ if (write(load, "0", 1) == -1)
+ ERROR("failed to write to %s\n", loadpath);
close(load);
DEBUG(2, "Done loading %s\n", path);
close(load);
DEBUG(2, "Done loading %s\n", path);
}
if (in->uid || in->gid) {
}
if (in->uid || in->gid) {
- setuid(in->uid);
- setgid(in->gid);
+ if (setuid(in->uid) || setgid(in->gid)) {
+ ERROR("failed to set uid:%d, gid:%d\n", in->uid, in->gid);
+ exit(127);
+ }
}
execvp(argv[0], argv);
exit(127);
}
execvp(argv[0], argv);
exit(127);
static void set_stdio(const char* tty)
{
static void set_stdio(const char* tty)
{
- chdir("/dev");
- freopen(tty, "r", stdin);
- freopen(tty, "w", stdout);
- freopen(tty, "w", stderr);
- chdir("/");
- fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) | O_NONBLOCK);
+ if (chdir("/dev") ||
+ !freopen(tty, "r", stdin) ||
+ !freopen(tty, "w", stdout) ||
+ !freopen(tty, "w", stderr) ||
+ chdir("/"))
+ ERROR("failed to set stdio\n");
+ else
+ fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) | O_NONBLOCK);
}
static void set_console(void)
}
static void set_console(void)
+ if (chdir("/dev")) {
+ ERROR("failed to change dir to /dev\n");
+ return;
+ }
while (tty!=NULL) {
f = open(tty, O_RDONLY);
if (f >= 0) {
while (tty!=NULL) {
f = open(tty, O_RDONLY);
if (f >= 0) {
+ if (chdir("/"))
+ ERROR("failed to change dir to /\n");
if (tty != NULL)
set_stdio(tty);
if (tty != NULL)
set_stdio(tty);
- asprintf(&json, "/tmp/%s.%u.json", basename(*argv), child);
+ if (asprintf(&json, "/tmp/%s.%u.json", basename(*argv), child) < 0)
+ ERROR("failed to allocate output path: %s\n", strerror(errno));
print_syscalls(policy, json);
print_syscalls(policy, json);
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libubox/uloop.h>
#include <libubox/uloop.h>
if (p != 1) {
fprintf(stderr, "this tool needs to run as pid 1\n");
return -1;
}
if (p != 1) {
fprintf(stderr, "this tool needs to run as pid 1\n");
return -1;
}
+ if (chdir("/tmp") == -1) {
+ fprintf(stderr, "failed to chdir to /tmp: %s\n", strerror(errno));
+ return -1;
+ }
if (argc != 2) {
fprintf(stderr, "sysupgrade stage 2 failed, no folder specified\n");
return -1;
if (argc != 2) {
fprintf(stderr, "sysupgrade stage 2 failed, no folder specified\n");
return -1;