Unverified Commit 3a9018bb by Stéphane Graber Committed by GitHub

Merge pull request #3463 from brauner/2020-06-26/fixes

confile: handle overflow in lxc.time.offset.{boot,monotonic}
parents 11e5f16a f1c43439
......@@ -2831,22 +2831,26 @@ static int set_config_time_offset_boot(const char *key, const char *value,
if (ret)
return ret;
/* TODO: Handle overflow. */
unit = lxc_trim_whitespace_in_place(buf);
if (strcmp(unit, "h") == 0)
lxc_conf->timens.s_boot = offset * 3600;
else if (strcmp(unit, "m") == 0)
lxc_conf->timens.s_boot = offset * 60;
else if (strcmp(unit, "s") == 0)
if (strcmp(unit, "h") == 0) {
if (!multiply_overflow(offset, 3600, &lxc_conf->timens.s_boot))
return -EOVERFLOW;
} else if (strcmp(unit, "m") == 0) {
if (!multiply_overflow(offset, 60, &lxc_conf->timens.s_boot))
return -EOVERFLOW;
} else if (strcmp(unit, "s") == 0) {
lxc_conf->timens.s_boot = offset;
else if (strcmp(unit, "ms") == 0)
lxc_conf->timens.ns_boot = offset * 1000000;
else if (strcmp(unit, "us") == 0)
lxc_conf->timens.ns_boot = offset * 1000;
else if (strcmp(unit, "ns") == 0)
} else if (strcmp(unit, "ms") == 0) {
if (!multiply_overflow(offset, 1000000, &lxc_conf->timens.ns_boot))
return -EOVERFLOW;
} else if (strcmp(unit, "us") == 0) {
if (!multiply_overflow(offset, 1000, &lxc_conf->timens.ns_boot))
return -EOVERFLOW;
} else if (strcmp(unit, "ns") == 0) {
lxc_conf->timens.ns_boot = offset;
else
} else {
return ret_errno(EINVAL);
}
return 0;
}
......@@ -2866,22 +2870,26 @@ static int set_config_time_offset_monotonic(const char *key, const char *value,
if (ret)
return ret;
// TODO: Handle overflow.
unit = lxc_trim_whitespace_in_place(buf);
if (strcmp(unit, "h") == 0)
lxc_conf->timens.s_monotonic = offset * 3600;
else if (strcmp(unit, "m") == 0)
lxc_conf->timens.s_monotonic = offset * 60;
else if (strcmp(unit, "s") == 0)
if (strcmp(unit, "h") == 0) {
if (!multiply_overflow(offset, 3600, &lxc_conf->timens.s_monotonic))
return -EOVERFLOW;
} else if (strcmp(unit, "m") == 0) {
if (!multiply_overflow(offset, 60, &lxc_conf->timens.s_monotonic))
return -EOVERFLOW;
} else if (strcmp(unit, "s") == 0) {
lxc_conf->timens.s_monotonic = offset;
else if (strcmp(unit, "ms") == 0)
lxc_conf->timens.ns_monotonic = offset * 1000000;
else if (strcmp(unit, "us") == 0)
lxc_conf->timens.ns_monotonic = offset * 1000;
else if (strcmp(unit, "ns") == 0)
} else if (strcmp(unit, "ms") == 0) {
if (!multiply_overflow(offset, 1000000, &lxc_conf->timens.ns_monotonic))
return -EOVERFLOW;
} else if (strcmp(unit, "us") == 0) {
if (!multiply_overflow(offset, 1000, &lxc_conf->timens.ns_monotonic))
return -EOVERFLOW;
} else if (strcmp(unit, "ns") == 0) {
lxc_conf->timens.ns_monotonic = offset;
else
} else {
return ret_errno(EINVAL);
}
return 0;
}
......
......@@ -1870,6 +1870,20 @@ static int lxc_spawn(struct lxc_handler *handler)
cgroup_ops->payload_finalize(cgroup_ops);
TRACE("Finished setting up cgroups");
if (handler->ns_clone_flags & CLONE_NEWTIME) {
/* Now we're ready to preserve the cgroup namespace */
ret = lxc_try_preserve_ns(handler->pid, "time");
if (ret < 0) {
if (ret != -EOPNOTSUPP) {
SYSERROR("Failed to preserve time namespace");
goto out_delete_net;
}
} else {
handler->nsfd[LXC_NS_TIME] = ret;
DEBUG("Preserved time namespace via fd %d", ret);
}
}
/* Run any host-side start hooks */
ret = run_lxc_hooks(name, "start-host", conf, NULL);
if (ret < 0) {
......
......@@ -1904,3 +1904,15 @@ int fix_stdio_permissions(uid_t uid)
return fret;
}
bool multiply_overflow(int64_t base, uint64_t mult, int64_t *res)
{
if (base > 0 && base > (INT64_MAX / mult))
return false;
if (base < 0 && base < (INT64_MIN / mult))
return false;
*res = base * mult;
return true;
}
......@@ -251,4 +251,6 @@ static inline bool gid_valid(gid_t gid)
return gid != LXC_INVALID_GID;
}
extern bool multiply_overflow(int64_t base, uint64_t mult, int64_t *res);
#endif /* __LXC_UTILS_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment