From owner-freebsd-security@freebsd.org Sat Oct 6 21:29:20 2018 Return-Path: Delivered-To: freebsd-security@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 802EB10C7475 for ; Sat, 6 Oct 2018 21:29:20 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 08EB285051 for ; Sat, 6 Oct 2018 21:29:19 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.15.2/8.15.2) with ESMTPS id w96LT93B083790 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO) for ; Sun, 7 Oct 2018 00:29:12 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua w96LT93B083790 Received: (from kostik@localhost) by tom.home (8.15.2/8.15.2/Submit) id w96LT9hA083789 for freebsd-security@freebsd.org; Sun, 7 Oct 2018 00:29:09 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Sun, 7 Oct 2018 00:29:08 +0300 From: Konstantin Belousov To: freebsd-security@freebsd.org Subject: Re: [FreeBSD-Announce] FreeBSD Security Advisory FreeBSD-SA-18:12.elf Message-ID: <20181006212908.GU5335@kib.kiev.ua> References: <20180912054309.61C6B13269@freefall.freebsd.org> <20181006173525.GC813@lena.kiev> <20181006182104.GS5335@kib.kiev.ua> <20181006184636.GT5335@kib.kiev.ua> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20181006184636.GT5335@kib.kiev.ua> User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FROM, NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on tom.home X-BeenThere: freebsd-security@freebsd.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: "Security issues \[members-only posting\]" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 06 Oct 2018 21:29:20 -0000 On Sat, Oct 06, 2018 at 09:46:36PM +0300, Konstantin Belousov wrote: > On Sat, Oct 06, 2018 at 09:21:04PM +0300, Konstantin Belousov wrote: > > On Sat, Oct 06, 2018 at 08:35:26PM +0300, Lena@lena.kiev.ua wrote: > > > > Insufficient validation was performed in the ELF header parser, and malformed > > > > or otherwise invalid ELF binaries were not rejected as they should be. > > > > > > What is invalid in the /usr/local/share/google-earth/googleearth-bin > > > binary of the port google-earth-7.1.5.1557,3 ? > > > > > > FreeBSD 11.2-RELEASE-p4 Sep 27 GENERIC i386, the binary: > > > https://drive.google.com/file/d/1SgHk8ijSp2F9UcQGlx44psT832TdIEL0/view > > > > > > ~ $ googleearth > > > Invalid PT_INTERP > > > exec: ./googleearth-bin: Exec format error > > > ~ $ readelf --program-headers /usr/local/share/google-earth/googleearth-bin > > > > > > Elf file type is EXEC (Executable file) > > > Entry point 0x8048650 > > > There are 8 program headers, starting at offset 52 > > > > > > Program Headers: > > > Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align > > > PHDR 0x000034 0x08048034 0x08048034 0x00100 0x00100 R E 0x4 > > > INTERP 0x000134 0x08048134 0x08048134 0x00011 0x00011 R 0x1 > > > [Requesting program interpreter: /lib/ld-linux.so.2] > > As you see, the file delcares that file/memory length of the interpreter > > name' segment is 0x11 == 16 decimal. But the string does not end on > > byte 16, which is not NUL. We tighten the checks and do require that > > PT_INTERP string is valid by checking that it is NUL-terminated at the > > offset declared by the size. > As emaste pointed out, I am off by one, i.e. replace 16 by 17 in the text > above. But we might be somewhat nicer in this case. Try the following. diff --git a/sys/kern/imgact_elf.c b/sys/kern/imgact_elf.c index f4302d46665..88f8a1ed2fa 100644 --- a/sys/kern/imgact_elf.c +++ b/sys/kern/imgact_elf.c @@ -872,9 +872,23 @@ __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp) interp = __DECONST(char *, imgp->image_header) + phdr[i].p_offset; if (interp[interp_name_len - 1] != '\0') { - uprintf("Invalid PT_INTERP\n"); - error = ENOEXEC; - goto ret; + /* + * ELF specification requires + * that PT_INTERP contained + * NUL-terminated string. If + * it is not, try to fix the + * path and still execute the + * binary. + */ + VOP_UNLOCK(imgp->vp, 0); + interp_buf = malloc(interp_name_len + 1, + M_TEMP, M_WAITOK); + vn_lock(imgp->vp, LK_EXCLUSIVE | + LK_RETRY); + memcpy(interp_buf, interp, + interp_name_len); + interp_buf[interp_name_len] = '\0'; + interp = interp_buf; } } break;