use strtoimax when needed
authorRobert Swiecki <robert@swiecki.net>
Sat, 26 May 2018 11:54:17 +0000 (13:54 +0200)
committerRobert Swiecki <robert@swiecki.net>
Sat, 26 May 2018 11:54:17 +0000 (13:54 +0200)
cmdline.cc
contain.cc
nsjail.h
subproc.cc
user.cc

index 83407f6d036d8be14aab43d55cd53002714256f4..6f5af8d9c3d6919b318890aeff665a116178b34c 100644 (file)
@@ -227,7 +227,7 @@ void logParams(nsjconf_t* nsjconf) {
 
        LOG_I(
            "Jail parameters: hostname:'%s', chroot:'%s', process:'%s', bind:[%s]:%d, "
-           "max_conns_per_ip:%u, time_limit:%ld, personality:%#lx, daemonize:%s, clone_newnet:%s, "
+           "max_conns_per_ip:%u, time_limit:%" PRId64 " , personality:%#lx, daemonize:%s, clone_newnet:%s, "
            "clone_newuser:%s, clone_newns:%s, clone_newpid:%s, clone_newipc:%s, clonew_newuts:%s, "
            "clone_newcgroup:%s, keep_caps:%s, disable_no_new_privs:%s, max_cpus:%zu",
            nsjconf->hostname.c_str(), nsjconf->chroot.c_str(),
@@ -291,7 +291,7 @@ uint64_t parseRLimit(int res, const char* optarg, unsigned long mul) {
        errno = 0;
        uint64_t val = strtoull(optarg, NULL, 0);
        if (val == ULLONG_MAX && errno != 0) {
-               PLOG_F("strtoul('%s', 0)", optarg);
+               PLOG_F("strtoull('%s', 0)", optarg);
        }
        return val * mul;
 }
@@ -499,7 +499,7 @@ std::unique_ptr<nsjconf_t> parseArgs(int argc, char* argv[]) {
                        nsjconf->chroot = optarg;
                        break;
                case 'p':
-                       nsjconf->port = strtoul(optarg, NULL, 0);
+                       nsjconf->port = strtoumax(optarg, NULL, 0);
                        nsjconf->mode = MODE_LISTEN_TCP;
                        break;
                case 0x604:
@@ -512,7 +512,7 @@ std::unique_ptr<nsjconf_t> parseArgs(int argc, char* argv[]) {
                        nsjconf->logfile = optarg;
                        break;
                case 'L':
-                       nsjconf->logfile = "/dev/fd/" + std::to_string(strtol(optarg, NULL, 10));
+                       nsjconf->logfile = "/dev/fd/" + std::to_string(strtoimax(optarg, NULL, 10));
                        break;
                case 'd':
                        nsjconf->daemonize = true;
@@ -533,7 +533,7 @@ std::unique_ptr<nsjconf_t> parseArgs(int argc, char* argv[]) {
                        nsjconf->keep_env = true;
                        break;
                case 't':
-                       nsjconf->tlimit = strtol(optarg, NULL, 0);
+                       nsjconf->tlimit = (uint64_t)strtoull(optarg, NULL, 0);
                        break;
                case 'h': /* help */
                        cmdlineUsage(argv[0]);
index ea33b42da82ec0f5d073d5b601720cab381e4187..bfc1d1f0cbebed850677b15090f2f67b46ae7f84 100644 (file)
@@ -220,28 +220,28 @@ static bool containMakeFdsCOEProc(nsjconf_t* nsjconf) {
                        continue;
                }
                errno = 0;
-               long fd = strtol(entry->d_name, NULL, 10);
-               if (fd == LONG_MAX && errno != 0) {
+               int fd = strtoimax(entry->d_name, NULL, 10);
+               if (errno != 0) {
                        PLOG_W("Cannot convert /proc/self/fd/%s to a number", entry->d_name);
                        continue;
                }
                int flags = TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
                if (flags == -1) {
-                       PLOG_D("fcntl(fd=%ld, F_GETFD, 0)", fd);
+                       PLOG_D("fcntl(fd=%xld, F_GETFD, 0)", fd);
                        closedir(dir);
                        return false;
                }
                if (containPassFd(nsjconf, fd)) {
-                       LOG_D("FD=%ld will be passed to the child process", fd);
+                       LOG_D("FD=%d will be passed to the child process", fd);
                        if (TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD, flags & ~(FD_CLOEXEC))) == -1) {
-                               PLOG_E("Could not clear FD_CLOEXEC for FD=%ld", fd);
+                               PLOG_E("Could not clear FD_CLOEXEC for FD=%d", fd);
                                closedir(dir);
                                return false;
                        }
                } else {
-                       LOG_D("FD=%ld will be closed before execve()", fd);
+                       LOG_D("FD=%d will be closed before execve()", fd);
                        if (TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD, flags | FD_CLOEXEC)) == -1) {
-                               PLOG_E("Could not set FD_CLOEXEC for FD=%ld", fd);
+                               PLOG_E("Could not set FD_CLOEXEC for FD=%d", fd);
                                closedir(dir);
                                return false;
                        }
index 7cccd689f6802f82430b479d17c0a4cb28a53046..99f904fc1439225ce69f08ced3210c5f5ad73499 100644 (file)
--- a/nsjail.h
+++ b/nsjail.h
@@ -96,7 +96,7 @@ struct nsjconf_t {
        std::string logfile;
        logs::llevel_t loglevel;
        bool daemonize;
-       time_t tlimit;
+       uint64_t tlimit;
        size_t max_cpus;
        bool keep_env;
        bool keep_caps;
index 2c7b1c1df205c63b33541ff1afd2a084f6fcec0b..3b969cbb757cb50a06dd642a96aaf8b1e1b58b73 100644 (file)
@@ -234,9 +234,9 @@ void displayProc(nsjconf_t* nsjconf) {
        time_t now = time(NULL);
        for (const auto& pid : nsjconf->pids) {
                time_t diff = now - pid.start;
-               time_t left = nsjconf->tlimit ? nsjconf->tlimit - diff : 0;
-               LOG_I("PID: %d, Remote host: %s, Run time: %ld sec. (time left: %ld sec.)", pid.pid,
-                   pid.remote_txt.c_str(), (long)diff, (long)left);
+               uint64_t left = nsjconf->tlimit ? nsjconf->tlimit - (uint64_t)diff : 0;
+               LOG_I("PID: %d, Remote host: %s, Run time: %ld sec. (time left: %" PRId64 " sec.)", pid.pid,
+                   pid.remote_txt.c_str(), (long)diff, left);
        }
 }
 
@@ -343,8 +343,8 @@ int reapProc(nsjconf_t* nsjconf) {
                }
                pid_t pid = p.pid;
                time_t diff = now - p.start;
-               if (diff >= nsjconf->tlimit) {
-                       LOG_I("PID: %d run time >= time limit (%ld >= %ld) (%s). Killing it", pid,
+               if ((uint64_t)diff >= nsjconf->tlimit) {
+                       LOG_I("PID: %d run time >= time limit (%ld >= %" PRId64 ") (%s). Killing it", pid,
                            (long)diff, (long)nsjconf->tlimit, p.remote_txt.c_str());
                        /*
                         * Probably a kernel bug - some processes cannot be killed with KILL if
diff --git a/user.cc b/user.cc
index 7c53a255bfa0ad3e347e66e15f71b02004469ef4..1eccadc8299304047dea73787d499711a3f151fa 100644 (file)
--- a/user.cc
+++ b/user.cc
@@ -274,7 +274,7 @@ static uid_t parseUid(const std::string& id) {
                return pw->pw_uid;
        }
        if (util::isANumber(id.c_str())) {
-               return (uid_t)strtoull(id.c_str(), NULL, 0);
+               return (uid_t)strtoimax(id.c_str(), NULL, 0);
        }
        return (uid_t)-1;
 }
@@ -288,7 +288,7 @@ static gid_t parseGid(const std::string& id) {
                return gr->gr_gid;
        }
        if (util::isANumber(id.c_str())) {
-               return (gid_t)strtoull(id.c_str(), NULL, 0);
+               return (gid_t)strtoimax(id.c_str(), NULL, 0);
        }
        return (gid_t)-1;
 }