From nobody Wed Mar 30 22:09:00 2022 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 732191A4FB8A; Wed, 30 Mar 2022 22:09:00 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4KTLDr2p0tz4k26; Wed, 30 Mar 2022 22:09:00 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1648678140; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Px/zCDrI0nLd3gt6CFCISOikFuhT9ztfIu4oRfc2PgU=; b=gmFrQjYKuZGI4tCKKuEbTlO1yFXbSbVwIesA8aHhJiR5qxu4QrgGYrOYcSX7PyeidVCPQM cX4RVNzkHH5GXzVuu2jJXwN2NCTjWdUMoxEjLb3em7duCSqAl9n4P0djy7wmtlu8xsyg+c He7+V0nM/B4lgPwCC1ZtsfA42H4+YnqdV+OW3I83YjngUS3G/wsP9y6+1ZuxkE3hgtgHRF osJsAgdAVbK9CjpdWr4rXZWG7SeepwMaKzBdwbSbvnr2BFSJlCLpfhlJFcFmqsto5nyYfl 60BvFg6hC4jl8CR4u90Ndrnu594E0019uD3PfOoZ45eEHsys/A8X/p8aYZ7Ccw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 3C7B81B621; Wed, 30 Mar 2022 22:09:00 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 22UM90mV034236; Wed, 30 Mar 2022 22:09:00 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 22UM90SB034235; Wed, 30 Mar 2022 22:09:00 GMT (envelope-from git) Date: Wed, 30 Mar 2022 22:09:00 GMT Message-Id: <202203302209.22UM90SB034235@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Vincenzo Maffione Subject: git: 725c70d8153f - stable/11 - netmap: Fix TOCTOU vulnerability in nmreq_copyin List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: vmaffione X-Git-Repository: src X-Git-Refname: refs/heads/stable/11 X-Git-Reftype: branch X-Git-Commit: 725c70d8153f4bddf95bdd07e2c7b4b9399643f6 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1648678140; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Px/zCDrI0nLd3gt6CFCISOikFuhT9ztfIu4oRfc2PgU=; b=ZUWS51ru3MKbIwY/VAQ742M2RboNmxokWzsb52K3QcJGx7Pv4o3XrmLNpq9djDW5eIkRea ZH1KT/MMXcx6GmBZ91QY7dW0TI4XxuAg0KhYS1VUUx4ONaf/U6Cc8V4styNFIVp+W9E18i yw770JfjrZJkSUTsgDfbgqH0Ot94YiHMdWd0l9yX3w2iZ/Ww8DUR5tbb6+tJUiARdG8xtX DSMd6kXOXh4Srm1Z9IrVLL8WeiyedPcAlzyqovC8gruOy0lBa8NG9n0Bmj/kRMg3RfsrYi 60CQfEKXXcPmHazUNsWz8Nk3v8SGUcjQxLootuv2iVsjkEXP6JxshjCx++xpRg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1648678140; a=rsa-sha256; cv=none; b=W7kHzwBwSlnP0qGWx3IQ0Bjtah/H4rM4miISufYAGpejwYfNBcNCiFRcm/Sxpu1A4VERaz 1coWW1nnMn9g0Pk9KfZorMTfRzEmgoc7T3gmJNLo+cqYMCkRtxKSnHQ7aPEmnUTXh94jZS 8AyNhiABta6ePYR12kYUurGUu5Iqsm+jiGUnxg7ZZ1iQia6rmjFVYvb8kCiWW+cVJ6Nquk UcJxEo/B/JFobhAHwpabecujVtWNHG7JyxqBu7AuPj7grpSe+wYMoAyz9Ys7DNqKg1rCBh +Q6cnCvHiggsbNzRkuBLH6w5Ue3qHcjP/JVib1k/ju97MN1Ng9mfzwtiajTZiQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/11 has been updated by vmaffione: URL: https://cgit.FreeBSD.org/src/commit/?id=725c70d8153f4bddf95bdd07e2c7b4b9399643f6 commit 725c70d8153f4bddf95bdd07e2c7b4b9399643f6 Author: Vincenzo Maffione AuthorDate: 2022-03-16 06:58:50 +0000 Commit: Vincenzo Maffione CommitDate: 2022-03-30 22:08:32 +0000 netmap: Fix TOCTOU vulnerability in nmreq_copyin The total size of the user-provided nmreq was first computed and then trusted during the copyin. This might lead to kernel memory corruption and escape from jails/containers. Reported by: Lucas Leong (@_wmliang_) of Trend Micro Zero Day Initiative Security: CVE-2022-23084 MFC after: 3 days (cherry picked from commit 393729916564ed13f966e09129a24e6931898d12) --- sys/dev/netmap/netmap.c | 46 ++++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/sys/dev/netmap/netmap.c b/sys/dev/netmap/netmap.c index 0c060219ff23..ab2c1b760d6a 100644 --- a/sys/dev/netmap/netmap.c +++ b/sys/dev/netmap/netmap.c @@ -2987,11 +2987,10 @@ nmreq_opt_size_by_type(uint32_t nro_reqtype, uint64_t nro_size) int nmreq_copyin(struct nmreq_header *hdr, int nr_body_is_user) { - size_t rqsz, optsz, bufsz, optbodysz; + size_t rqsz, optsz, bufsz; int error = 0; char *ker = NULL, *p; struct nmreq_option **next, *src; - struct nmreq_option buf; uint64_t *ptrs; if (hdr->nr_reserved) { @@ -3021,32 +3020,14 @@ nmreq_copyin(struct nmreq_header *hdr, int nr_body_is_user) goto out_err; } - bufsz = 2 * sizeof(void *) + rqsz; - optsz = 0; - for (src = (struct nmreq_option *)(uintptr_t)hdr->nr_options; src; - src = (struct nmreq_option *)(uintptr_t)buf.nro_next) - { - error = copyin(src, &buf, sizeof(*src)); - if (error) - goto out_err; - /* Validate nro_size to avoid integer overflow of optsz and bufsz. */ - if (buf.nro_size > NETMAP_REQ_MAXSIZE) { - error = EMSGSIZE; - goto out_err; - } - optsz += sizeof(*src); - optbodysz = nmreq_opt_size_by_type(buf.nro_reqtype, buf.nro_size); - if (optbodysz > NETMAP_REQ_MAXSIZE) { - error = EMSGSIZE; - goto out_err; - } - optsz += optbodysz; - if (rqsz + optsz > NETMAP_REQ_MAXSIZE) { - error = EMSGSIZE; - goto out_err; - } - bufsz += optsz + sizeof(void *); - } + /* + * The buffer size must be large enough to store the request body, + * all the possible options and the additional user pointers + * (2+NETMAP_REQ_OPT_MAX). Note that the maximum size of body plus + * options can not exceed NETMAP_REQ_MAXSIZE; + */ + bufsz = (2 + NETMAP_REQ_OPT_SYNC_KLOOP_MODE + 1) * sizeof(void *) + + NETMAP_REQ_MAXSIZE; ker = nm_os_malloc(bufsz); if (ker == NULL) { @@ -3081,6 +3062,7 @@ nmreq_copyin(struct nmreq_header *hdr, int nr_body_is_user) error = copyin(src, opt, sizeof(*src)); if (error) goto out_restore; + rqsz += sizeof(*src); /* make a copy of the user next pointer */ *ptrs = opt->nro_next; /* overwrite the user pointer with the in-kernel one */ @@ -3096,6 +3078,14 @@ nmreq_copyin(struct nmreq_header *hdr, int nr_body_is_user) /* copy the option body */ optsz = nmreq_opt_size_by_type(opt->nro_reqtype, opt->nro_size); + /* check optsz and nro_size to avoid for possible integer overflows of rqsz */ + if ((optsz > NETMAP_REQ_MAXSIZE) || (opt->nro_size > NETMAP_REQ_MAXSIZE) + || (rqsz + optsz > NETMAP_REQ_MAXSIZE) + || (optsz > 0 && rqsz + optsz <= rqsz)) { + error = EMSGSIZE; + goto out_restore; + } + rqsz += optsz; if (optsz) { /* the option body follows the option header */ error = copyin(src + 1, p, optsz);