From nobody Sun Dec 19 02:44:30 2021 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 5516C18E48D5; Sun, 19 Dec 2021 02:44:31 +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 4JGn9p57C0z4qfq; Sun, 19 Dec 2021 02:44:30 +0000 (UTC) (envelope-from git@FreeBSD.org) 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 5F9FA1AE9D; Sun, 19 Dec 2021 02:44:30 +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 1BJ2iUgo012638; Sun, 19 Dec 2021 02:44:30 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1BJ2iUFa012637; Sun, 19 Dec 2021 02:44:30 GMT (envelope-from git) Date: Sun, 19 Dec 2021 02:44:30 GMT Message-Id: <202112190244.1BJ2iUFa012637@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: 681e834b02fd - stable/13 - imgact_elf: exclude invalid alignment requests 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: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 681e834b02fd00a9dd3c55f820538bf3674a4a4b Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1639881870; 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=ci5Vti1Ppxpi6RzrQET0D5VeDPTNG0JAIPKRsYtWiJo=; b=W9Wh8po4Ao4bD2xSiEhZDU6eSiIWEk1UOUkLT1cuvZmIxeA6xwxfe/Fr8YaV2WBLH3vkgZ NRKitZ3UUw86l48Ovwg36/c/HzM/8kQIA3nMzeMSU7teqey7XcgzT0Lb79I5bmwQxwGlIK gvRbmh9ojz6kyqgi9XI/W+Pl/8YQ5qWyCc0cppp15PEws+QhK6qnxm13nl0VxqwvH17DOg Cs8fiaLkGa5e4pxMHB54KZLhewrxF428Tiah4cC4zcYsPRsgEZxBkkF0xiw2/dvj72AB5r cqGC7PBYWEcIksejD67TUtkKafZyXB1tVOUyN7wyL7bSHCx4T8I7QFCnFviHxw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1639881870; a=rsa-sha256; cv=none; b=hwA5iGR5q6DwMqo4b9LGq8gyJxtXh2iGeu55Du+IGBn4szGGrzcbz0JIrQGYGUsrb6h2lA 9eGU0c+wkSmsqHRmFZdILXmlK4lcMXZNzUS5A6GKZFwcUh/oKJLJQW5yt6CdD3tkirnxB7 x2Wx+f2ikby3+15GMJHEL4yrjMNT+/2QUoXIQricuEW9BBawpaQQaBxna8E7eRtfM1fPIf ckgSonCI7tfae8M0ul1697NNG2cbwpKqa0sjXqvo/DkQGiM2ZIVDKrlONUvmmCjbaikmDS kZTlT4kTDRkqHODoxdjAuZqXMDqa2zPG26ddDEjATKxnTBZyn4yxaoNIPbcKqw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=681e834b02fd00a9dd3c55f820538bf3674a4a4b commit 681e834b02fd00a9dd3c55f820538bf3674a4a4b Author: Konstantin Belousov AuthorDate: 2021-12-08 09:33:57 +0000 Commit: Konstantin Belousov CommitDate: 2021-12-19 02:42:51 +0000 imgact_elf: exclude invalid alignment requests (cherry picked from commit 714d6d09b57e9cf98a5c3f45f869c08be5454849) --- sys/kern/imgact_elf.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/sys/kern/imgact_elf.c b/sys/kern/imgact_elf.c index 77ecd1538084..e0dd2b13c143 100644 --- a/sys/kern/imgact_elf.c +++ b/sys/kern/imgact_elf.c @@ -1092,7 +1092,7 @@ __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp) Elf_Brandinfo *brand_info; struct sysentvec *sv; u_long addr, baddr, et_dyn_addr, entry, proghdr; - u_long maxalign, mapsz, maxv, maxv1; + u_long maxalign, maxsalign, mapsz, maxv, maxv1; uint32_t fctl0; int32_t osrel; bool free_interp; @@ -1133,7 +1133,20 @@ __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp) interp = NULL; free_interp = false; td = curthread; + + /* + * Somewhat arbitrary, limit accepted max alignment for the + * loadable segment to the max supported superpage size. Too + * large alignment requests are not useful and are indicators + * of corrupted or outright malicious binary. + */ maxalign = PAGE_SIZE; + maxsalign = PAGE_SIZE * 1024; + for (i = MAXPAGESIZES - 1; i > 0; i--) { + if (pagesizes[i] > maxsalign) + maxsalign = pagesizes[i]; + } + mapsz = 0; for (i = 0; i < hdr->e_phnum; i++) { @@ -1141,6 +1154,11 @@ __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp) case PT_LOAD: if (n == 0) baddr = phdr[i].p_vaddr; + if (phdr[i].p_align > maxsalign) { + uprintf("Invalid segment alignment\n"); + error = ENOEXEC; + goto ret; + } if (phdr[i].p_align > maxalign) maxalign = phdr[i].p_align; mapsz += phdr[i].p_memsz;