From nobody Sun Dec 12 14:30:35 2021 X-Original-To: dev-commits-src-all@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 E13C618E8D77; Sun, 12 Dec 2021 14:30:37 +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 4JBn9l5WxYz4mWt; Sun, 12 Dec 2021 14:30:35 +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 43A74229D5; Sun, 12 Dec 2021 14:30:35 +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 1BCEUZR4066695; Sun, 12 Dec 2021 14:30:35 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1BCEUZNh066694; Sun, 12 Dec 2021 14:30:35 GMT (envelope-from git) Date: Sun, 12 Dec 2021 14:30:35 GMT Message-Id: <202112121430.1BCEUZNh066694@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 714d6d09b57e - main - imgact_elf: exclude invalid alignment requests List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@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/main X-Git-Reftype: branch X-Git-Commit: 714d6d09b57e9cf98a5c3f45f869c08be5454849 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1639319436; 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=jMRGp0ayW3BToc837/os/RoXM89tLUi3F2pvOrCn7no=; b=u1CAEmmTkgjPJ+5poMMdRBbeBvoDIAGB9eqOM/e0VydMmbuNXUlMOph95Pp55H1h35CdKU tF4jhEHWue/BGlpZJeQlbtt+Y72keznuvAMsdfOVRUp+Xkq60ho/pVS564bho26ACGTrWL dCmVnbLybRMQXuLx8h2HYJIsBNNxrlmMexQpyKbb9QviCyMcox1q8XsCjRwPQS1Bwb9Pmx cy4XGcA5sTM+X02UuhMfC/YoCCbsVsNj4zATNEcU4qWgwOzU6bv7qXMSn1znTP7bnrH5Wz tCt0zpQ1PrJfH6/r1je4xpBXArksSCKeN82S7WZAkP28kDsdnk+MUbRsvHthgw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1639319436; a=rsa-sha256; cv=none; b=UCR45HxGBE0/JYB9DlRMAnLhewqRQfJG3YmTIOgRrhDgKLIvzNE5vXrouIsr7HuDPXr8oa zQaR9uF8mhLsZI+9YO8EwJajReU8W/Gvyk5aIc6t28ohDbHigtHq8CR7JV5XKmO9WOawLk AzLQ6bkEQKprEUzWf/bxfreVf8w6wbt+zY8beoIasXyom5qLSOmgvj7IxSWJF1uD0DKBeS irXLICm/dQc5pRTjNB4eduOBdtVwqQOjTzgB6BKFeyt1IjXo41NeCqM0JfgkROHyn3aEdI BsKx6LE6EY5/O/Rxv9Z2mhLQMB5hAR8In6jlEZOMtMzO49/18Bo9eMx2Ch/sdA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=714d6d09b57e9cf98a5c3f45f869c08be5454849 commit 714d6d09b57e9cf98a5c3f45f869c08be5454849 Author: Konstantin Belousov AuthorDate: 2021-12-08 09:33:57 +0000 Commit: Konstantin Belousov CommitDate: 2021-12-12 14:28:38 +0000 imgact_elf: exclude invalid alignment requests Only accept at most superpage alignment, or if the arch does not have superpages supported, artificially limit it to PAGE_SIZE * 1024. This is somewhat arbitrary, and e.g. could change what binaries do we accept between native i386 vs. amd64 ia32 with superpages disabled, but I do not believe the difference there is affecting anybody with real (useful) binaries. Reported and tested by: pho Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D33359 --- 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 7e7d4bdc1647..3b05b38f3ed1 100644 --- a/sys/kern/imgact_elf.c +++ b/sys/kern/imgact_elf.c @@ -1112,7 +1112,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; @@ -1153,7 +1153,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++) { @@ -1161,6 +1174,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;