Skip to content

Commit

Permalink
Merge pull request lxc#4418 from mihalicyn/cumulative_fixes_2apr2024
Browse files Browse the repository at this point in the history
confile_utils: fix incorrect multiply_overflow test #2
  • Loading branch information
stgraber authored Apr 2, 2024
2 parents b7707ce + 630b46e commit b0fd9a9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 deletions.
24 changes: 12 additions & 12 deletions src/lxc/confile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2963,18 +2963,18 @@ static int set_config_time_offset_boot(const char *key, const char *value,

unit = lxc_trim_whitespace_in_place(buf);
if (strequal(unit, "h")) {
if (!multiply_overflow(offset, 3600, &lxc_conf->timens.s_boot))
if (check_mul_overflow(offset, (typeof(offset))3600, &lxc_conf->timens.s_boot))
return ret_errno(EOVERFLOW);
} else if (strequal(unit, "m")) {
if (!multiply_overflow(offset, 60, &lxc_conf->timens.s_boot))
if (check_mul_overflow(offset, (typeof(offset))60, &lxc_conf->timens.s_boot))
return ret_errno(EOVERFLOW);
} else if (strequal(unit, "s")) {
lxc_conf->timens.s_boot = offset;
} else if (strequal(unit, "ms")) {
if (!multiply_overflow(offset, 1000000, &lxc_conf->timens.ns_boot))
if (check_mul_overflow(offset, (typeof(offset))1000000, &lxc_conf->timens.ns_boot))
return ret_errno(EOVERFLOW);
} else if (strequal(unit, "us")) {
if (!multiply_overflow(offset, 1000, &lxc_conf->timens.ns_boot))
if (check_mul_overflow(offset, (typeof(offset))1000, &lxc_conf->timens.ns_boot))
return ret_errno(EOVERFLOW);
} else if (strequal(unit, "ns")) {
lxc_conf->timens.ns_boot = offset;
Expand Down Expand Up @@ -3002,18 +3002,18 @@ static int set_config_time_offset_monotonic(const char *key, const char *value,

unit = lxc_trim_whitespace_in_place(buf);
if (strequal(unit, "h")) {
if (!multiply_overflow(offset, 3600, &lxc_conf->timens.s_monotonic))
if (check_mul_overflow(offset, (typeof(offset))3600, &lxc_conf->timens.s_monotonic))
return ret_errno(EOVERFLOW);
} else if (strequal(unit, "m")) {
if (!multiply_overflow(offset, 60, &lxc_conf->timens.s_monotonic))
if (check_mul_overflow(offset, (typeof(offset))60, &lxc_conf->timens.s_monotonic))
return ret_errno(EOVERFLOW);
} else if (strequal(unit, "s")) {
lxc_conf->timens.s_monotonic = offset;
} else if (strequal(unit, "ms")) {
if (!multiply_overflow(offset, 1000000, &lxc_conf->timens.ns_monotonic))
if (check_mul_overflow(offset, (typeof(offset))1000000, &lxc_conf->timens.ns_monotonic))
return ret_errno(EOVERFLOW);
} else if (strequal(unit, "us")) {
if (!multiply_overflow(offset, 1000, &lxc_conf->timens.ns_monotonic))
if (check_mul_overflow(offset, (typeof(offset))1000, &lxc_conf->timens.ns_monotonic))
return ret_errno(EOVERFLOW);
} else if (strequal(unit, "ns")) {
lxc_conf->timens.ns_monotonic = offset;
Expand Down Expand Up @@ -4726,9 +4726,9 @@ static int get_config_time_offset_boot(const char *key, char *retv, int inlen, s
memset(retv, 0, inlen);

if (c->timens.s_boot) {
strprint(retv, inlen, "%" PRId64 " s\n", c->timens.s_boot);
strprint(retv, inlen, "%" PRId64 "s", c->timens.s_boot);
} else {
strprint(retv, inlen, "%" PRId64 " ns\n", c->timens.ns_boot);
strprint(retv, inlen, "%" PRId64 "ns", c->timens.ns_boot);
}

return fulllen;
Expand All @@ -4746,9 +4746,9 @@ static int get_config_time_offset_monotonic(const char *key, char *retv, int inl
memset(retv, 0, inlen);

if (c->timens.s_monotonic) {
strprint(retv, inlen, "%" PRId64 "s\n", c->timens.s_monotonic);
strprint(retv, inlen, "%" PRId64 "s", c->timens.s_monotonic);
} else {
strprint(retv, inlen, "%" PRId64 "ns\n", c->timens.ns_monotonic);
strprint(retv, inlen, "%" PRId64 "ns", c->timens.ns_monotonic);
}

return fulllen;
Expand Down
12 changes: 0 additions & 12 deletions src/lxc/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -2071,18 +2071,6 @@ 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_t)(INT64_MAX / mult))
return false;

if (base < 0 && base < (int64_t)(INT64_MIN / mult))
return false;

*res = (int64_t)(base * mult);
return true;
}

int print_r(int fd, const char *path)
{
__do_close int dfd = -EBADF, dfd_dup = -EBADF;
Expand Down
2 changes: 0 additions & 2 deletions src/lxc/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,6 @@ static inline bool gid_valid(gid_t gid)
return gid != LXC_INVALID_GID;
}

__hidden extern bool multiply_overflow(int64_t base, uint64_t mult, int64_t *res);

__hidden extern int safe_mount_beneath(const char *beneath, const char *src, const char *dst,
const char *fstype, unsigned int flags, const void *data);
__hidden extern int safe_mount_beneath_at(int beneat_fd, const char *src, const char *dst,
Expand Down
35 changes: 30 additions & 5 deletions src/tests/parse_config_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -906,12 +906,12 @@ int main(int argc, char *argv[])
}

if (c->set_config_item(c, "lxc.hook.version", "2")) {
lxc_error("%s\n", "Managed to set to set invalid config item \"lxc.hook.version\" to \"2\"");
lxc_error("%s\n", "Managed to set invalid config item \"lxc.hook.version\" to \"2\"");
goto non_test_error;
}

if (!c->set_config_item(c, "lxc.monitor.signal.pdeath", "SIGKILL")) {
lxc_error("%s\n", "Failed to set to set invalid config item \"lxc.monitor.signal.pdeath\" to \"SIGKILL\"");
lxc_error("%s\n", "Failed to set invalid config item \"lxc.monitor.signal.pdeath\" to \"SIGKILL\"");
goto non_test_error;
}

Expand All @@ -921,17 +921,17 @@ int main(int argc, char *argv[])
}

if (c->set_config_item(c, "lxc.notaconfigkey", "invalid")) {
lxc_error("%s\n", "Managed to set to set invalid config item \"lxc.notaconfigkey\" to \"invalid\"");
lxc_error("%s\n", "Managed to set invalid config item \"lxc.notaconfigkey\" to \"invalid\"");
return -1;
}

if (c->set_config_item(c, "lxc.log.file=", "./")) {
lxc_error("%s\n", "Managed to set to set invalid config item \"lxc.log.file\" to \"./\"");
lxc_error("%s\n", "Managed to set invalid config item \"lxc.log.file\" to \"./\"");
return -1;
}

if (c->set_config_item(c, "lxc.hook.versionasdfsadfsadf", "1")) {
lxc_error("%s\n", "Managed to set to set invalid config item \"lxc.hook.versionasdfsadfsadf\" to \"2\"");
lxc_error("%s\n", "Managed to set invalid config item \"lxc.hook.versionasdfsadfsadf\" to \"2\"");
goto non_test_error;
}

Expand All @@ -940,6 +940,31 @@ int main(int argc, char *argv[])
goto non_test_error;
}

if (set_get_compare_clear_save_load(c, "lxc.time.offset.boot", "-1234s", tmpf, true) < 0) {
lxc_error("%s\n", "lxc.time.offset.boot");
goto non_test_error;
}

if (!c->set_config_item(c, "lxc.time.offset.boot", "1000000000000000us")) {
lxc_error("%s\n", "Failed to set a valid value (1000000000000000us) to config item \"lxc.time.offset.boot\"");
goto non_test_error;
}

if (!c->set_config_item(c, "lxc.time.offset.boot", "-1000000000000000us")) {
lxc_error("%s\n", "Failed to set a valid (-1000000000000000us) value to config item \"lxc.time.offset.boot\"");
goto non_test_error;
}

if (c->set_config_item(c, "lxc.time.offset.boot", "10000000000000000us")) {
lxc_error("%s\n", "Managed to set overflowed value to config item \"lxc.time.offset.boot\"");
goto non_test_error;
}

if (set_get_compare_clear_save_load(c, "lxc.time.offset.monotonic", "4321s", tmpf, true) < 0) {
lxc_error("%s\n", "lxc.time.offset.monotonic");
goto non_test_error;
}

fret = EXIT_SUCCESS;

non_test_error:
Expand Down

0 comments on commit b0fd9a9

Please sign in to comment.