Skip to content

Commit

Permalink
net: Fix TOCTOU race condition in unix_conf_op
Browse files Browse the repository at this point in the history
The unix_conf_op function reads the size of the sysctl entry array
twice. gcc thinks that it can lead to a time-of-check to time-of-use
(TOCTOU) race condition if the array size changes between the two reads.

Fixes #2398

Signed-off-by: Andrei Vagin <avagin@gmail.com>
  • Loading branch information
avagin committed May 28, 2024
1 parent 7de0b45 commit 9401482
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
15 changes: 8 additions & 7 deletions criu/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,22 +359,23 @@ static int ipv6_conf_op(char *tgt, SysctlEntry **conf, int n, int op, SysctlEntr
return net_conf_op(tgt, conf, n, op, "ipv6", req, path, ARRAY_SIZE(devconfs6), devconfs6, def_conf);
}

static int unix_conf_op(SysctlEntry ***rconf, size_t *n, int op)
static int unix_conf_op(SysctlEntry ***rconf, size_t *pn, int op)
{
int i, ret = -1, flags = 0;
char path[ARRAY_SIZE(unix_conf_entries)][MAX_CONF_UNIX_PATH] = {};
struct sysctl_req req[ARRAY_SIZE(unix_conf_entries)] = {};
SysctlEntry **conf = *rconf;
size_t n = *pn;

if (*n != ARRAY_SIZE(unix_conf_entries)) {
pr_err("unix: Unexpected entries in config (%zu %zu)\n", *n, ARRAY_SIZE(unix_conf_entries));
if (n != ARRAY_SIZE(unix_conf_entries)) {
pr_err("unix: Unexpected entries in config (%zu %zu)\n", n, ARRAY_SIZE(unix_conf_entries));
return -EINVAL;
}

if (opts.weak_sysctls || op == CTL_READ)
flags = CTL_FLAGS_OPTIONAL;

for (i = 0; i < *n; i++) {
for (i = 0; i < n; i++) {
snprintf(path[i], MAX_CONF_UNIX_PATH, CONF_UNIX_FMT, unix_conf_entries[i]);
req[i].name = path[i];
req[i].flags = flags;
Expand All @@ -390,7 +391,7 @@ static int unix_conf_op(SysctlEntry ***rconf, size_t *n, int op)
}
}

ret = sysctl_op(req, *n, op, CLONE_NEWNET);
ret = sysctl_op(req, n, op, CLONE_NEWNET);
if (ret < 0) {
pr_err("unix: Failed to %s %s/<confs>\n", (op == CTL_READ) ? "read" : "write", CONF_UNIX_BASE);
return -1;
Expand All @@ -399,7 +400,7 @@ static int unix_conf_op(SysctlEntry ***rconf, size_t *n, int op)
if (op == CTL_READ) {
bool has_entries = false;

for (i = 0; i < *n; i++) {
for (i = 0; i < n; i++) {
if (req[i].flags & CTL_FLAGS_HAS) {
conf[i]->has_iarg = true;
if (!has_entries)
Expand All @@ -412,7 +413,7 @@ static int unix_conf_op(SysctlEntry ***rconf, size_t *n, int op)
* Unix conf is optional.
*/
if (!has_entries) {
*n = 0;
*pn = 0;
*rconf = NULL;
}
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/build/Dockerfile.x86_64.hdr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ubuntu:focal
FROM ubuntu:24.04

COPY scripts/ci/apt-install /bin/apt-install

Expand Down

0 comments on commit 9401482

Please sign in to comment.