From nobody Wed Mar 16 07:08:47 2022 X-Original-To: dev-commits-src-main@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 B5F611A2E660; Wed, 16 Mar 2022 07:08:47 +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 4KJLwb4bbLz4nZ8; Wed, 16 Mar 2022 07:08:47 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1647414527; 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=Pb0rB5rgz6rUWBZCc9282nTukiZUtQcMpA8g4ywdufM=; b=mWbl11O/pdAF1UkNDjXI6LvkKzdY3Bp8YUwZZsNesrd5OOlqZeoVVjfYZBERu9NBB5aE2q TP6TE7Fj6llPHhLL1bkKgQ+SEW3tct+I4owkDdm7E7ipCrfTqdxfmcT8Z9J0mReMjHFY2A wSpMgDtB3+7j/rStHNH0x3sLZdjhsMdSsGsyUOCoPF34fxwlGPeoO2illd39XHoEm2dLIr o6UDheR+ksOb9kW5CTRqqFPT2PHVFx37Xz9+NZikBtCkK1UgOysl56mPklJSyU5s6pqPFi 8maGMKZGI84EVaIbur+4rGkBlVrmqZIl74WydVfGzSy2cxNi2qMxpoIdV8n3WQ== 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 7EB4F11E19; Wed, 16 Mar 2022 07:08:47 +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 22G78lHo012260; Wed, 16 Mar 2022 07:08:47 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 22G78lBs012259; Wed, 16 Mar 2022 07:08:47 GMT (envelope-from git) Date: Wed, 16 Mar 2022 07:08:47 GMT Message-Id: <202203160708.22G78lBs012259@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Vincenzo Maffione Subject: git: 393729916564 - main - netmap: Fix TOCTOU vulnerability in nmreq_copyin List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: 393729916564ed13f966e09129a24e6931898d12 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1647414527; 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=Pb0rB5rgz6rUWBZCc9282nTukiZUtQcMpA8g4ywdufM=; b=XV6r11ElqxnlUvCW4SXxNOI3sNZcHY85firKPniKunZjNOm3t4GNRZxTigOGFNY/CVRhd7 4SezHYRdfBAoHDA2SQlN75XOJG+oxc2+QWbrTqwE3WX14q/kWAzvwBMsJzhut9pGsQfO/Y j87afoUDito9r4Mfc0GBvoahHHkTxxjsQighnhUQSY/bI4NuhZXm91Q9N3Gu5ieFtUbbHE iq2+uoCmYk+0OOFzCpOnQUlEfg/LGJkBoWVo3S/wao2LpjJA8N2LB8APelmmOSxJtEnEZs JexPK6d0Cn+CRHEwv/y34SHVqu/RNA/YIhf4gZ+JJy8DyiK3FpecU3E65yAQJQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1647414527; a=rsa-sha256; cv=none; b=a9mA4wdkkw5Yf8hx5kSXj1fCR+wG4XXQDBee1uiisWjk+AeTqER0Ai+beazGvI04L8iruC mAWZxTNQZ05A9gLs5E8Lr69bLaVexOgg17TfpnhDY1qDnGA0ZqbYkpOy8SucxQWYJjEtrR FTDpYXR1DAeAvLZNNfhbxjDg297rLT5+b/WlRTxIwGdaK69v+Mp5Mkw58FEQpX+KTPOUlV Lv1GvDZhNR1NVHBZdziQkV6bMiYbann5jAwiZpJJ5BesQRu26lh+yt1J0KLI2XcSz0docF kQz2daJnkgZKbpLRtuxzoUaIUgdxN1YzIznFO8PA1U7LlwcbCNrHNig7w5d2jw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by vmaffione: URL: https://cgit.FreeBSD.org/src/commit/?id=393729916564ed13f966e09129a24e6931898d12 commit 393729916564ed13f966e09129a24e6931898d12 Author: Vincenzo Maffione AuthorDate: 2022-03-16 06:58:50 +0000 Commit: Vincenzo Maffione CommitDate: 2022-03-16 06:58:50 +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 --- sys/dev/netmap/netmap.c | 51 +++++++++++++++++-------------------------------- 1 file changed, 17 insertions(+), 34 deletions(-) diff --git a/sys/dev/netmap/netmap.c b/sys/dev/netmap/netmap.c index b062b1ba7737..a166bc9d233d 100644 --- a/sys/dev/netmap/netmap.c +++ b/sys/dev/netmap/netmap.c @@ -3362,11 +3362,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, **opt_tab; - struct nmreq_option buf; uint64_t *ptrs; if (hdr->nr_reserved) { @@ -3396,39 +3395,14 @@ nmreq_copyin(struct nmreq_header *hdr, int nr_body_is_user) goto out_err; } - bufsz = 2 * sizeof(void *) + rqsz + - NETMAP_REQ_OPT_MAX * sizeof(opt_tab); - /* compute the size of the buf below the option table. - * It must contain a copy of every received option structure. - * For every option we also need to store a copy of the user - * list pointer. + /* + * 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; */ - 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 += sizeof(void *); - } - bufsz += optsz; + bufsz = (2 + NETMAP_REQ_OPT_MAX) * sizeof(void *) + NETMAP_REQ_MAXSIZE + + NETMAP_REQ_OPT_MAX * sizeof(opt_tab); ker = nm_os_malloc(bufsz); if (ker == NULL) { @@ -3466,6 +3440,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 */ @@ -3509,6 +3484,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);