Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp56930imm; Thu, 7 Jun 2018 13:45:06 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJHDofeCjnis6xbTKAaFW0f+htWd0HkS854K0zuLedTc8T5Z1shHv+xb7P7iVgjPxI5eis4 X-Received: by 2002:a17:902:31a4:: with SMTP id x33-v6mr3534931plb.355.1528404306858; Thu, 07 Jun 2018 13:45:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528404306; cv=none; d=google.com; s=arc-20160816; b=woXS/QahasebiAMJ8ozgIx7R7sQni6BzqLCK225WrSoKWuW18kSOPx6ONCVOsZPQ1T zW239sQIBpFZGjXnpiQkDIkO2PlbNhmbd2GT7+NIm+Orb6z3wa3jr8pvpdPq1NtMCxmF Wy1gADwmeSHxqdHGGJcV3izb4naAwAtejyal+mtaygVazLoKFlKxnBnFc6hEbSWWXy7a +pDlG8z64Qj4DOhE5zkgQA6VyYN9LBbYj/q+GG/XCcJygdmqd3+R2iyoklxagQvTf6rt R34Hg8RAdpFtEuZqBys/SktsE5jitZNe8EHIg2lFgdnPbIrWTFqc1V8CYpCCgeiq9MaI htqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:date:cc:to:from:subject:message-id :arc-authentication-results; bh=kSNlquCW/Gvs5pkQ/nMGJIIKrJc+/RPlvUDmD3PyvhE=; b=sMIs4ez0GO3vgcWXOeoGlyYgxiYIp64blvcbS12WY0iQreq3OzT1/3K93co5cLq5eE //3wh4JfONQ4MgbZqpJVh54lA8yZnl4FiyY3CyHt1vMKVeIwPBCVKuca6blCNaWfA4eR mChXIp4SiqvgSe7whE2LiasIpL50XFmNqGY+yPHzv7HZI+OZFaiFTJrO5WDJIjeZzHX3 7kawND/GWKFFmwnXwQmkx1aWXUANTojtjpoJG0ljh69T8iBV8mpvSxTbAFbYJw5eBsrA cg+B6rlwrV+OmJb5nr5v98aRZy3oYw8pxM5y0Y2oV9CzegB17CT++T5/i/F/09QKZQKp vPZg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j66-v6si5879925pfc.243.2018.06.07.13.44.52; Thu, 07 Jun 2018 13:45:06 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932553AbeFGUna (ORCPT + 99 others); Thu, 7 Jun 2018 16:43:30 -0400 Received: from mga09.intel.com ([134.134.136.24]:55802 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750892AbeFGUn2 (ORCPT ); Thu, 7 Jun 2018 16:43:28 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 07 Jun 2018 13:43:28 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,488,1520924400"; d="scan'208";a="47626651" Received: from 2b52.sc.intel.com (HELO [143.183.136.51]) ([143.183.136.51]) by orsmga008.jf.intel.com with ESMTP; 07 Jun 2018 13:43:27 -0700 Message-ID: <1528404016.5646.0.camel@2b52.sc.intel.com> Subject: Re: [PATCH 05/10] x86/cet: ELF header parsing of Control Flow Enforcement From: Yu-cheng Yu To: Andy Lutomirski Cc: LKML , linux-doc@vger.kernel.org, Linux-MM , linux-arch , X86 ML , "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , "H. J. Lu" , "Shanbhogue, Vedvyas" , "Ravi V. Shankar" , Dave Hansen , Jonathan Corbet , Oleg Nesterov , Arnd Bergmann , mike.kravetz@oracle.com Date: Thu, 07 Jun 2018 13:40:16 -0700 In-Reply-To: References: <20180607143807.3611-1-yu-cheng.yu@intel.com> <20180607143807.3611-6-yu-cheng.yu@intel.com> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.10.4-0ubuntu2 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 2018-06-07 at 11:38 -0700, Andy Lutomirski wrote: > On Thu, Jun 7, 2018 at 7:41 AM Yu-cheng Yu wrote: > > > > Look in .note.gnu.property of an ELF file and check if shadow stack needs > > to be enabled for the task. > > Nice! But please structure it so it's one function that parses out > all the ELF notes and some other code (a table or a switch statement) > that handles them. We will probably want to add more kernel-parsed > ELF notes some day, so let's structure the code to make it easier. > > > +static int find_cet(u8 *buf, u32 size, u32 align, int *shstk, int *ibt) > > +{ > > + unsigned long start = (unsigned long)buf; > > + struct elf_note *note = (struct elf_note *)buf; > > + > > + *shstk = 0; > > + *ibt = 0; > > + > > + /* > > + * Go through the x86_note_gnu_property array pointed by > > + * buf and look for shadow stack and indirect branch > > + * tracking features. > > + * The GNU_PROPERTY_X86_FEATURE_1_AND entry contains only > > + * one u32 as data. Do not go beyond buf_size. > > + */ > > + > > + while ((unsigned long) (note + 1) - start < size) { > > + /* Find the NT_GNU_PROPERTY_TYPE_0 note. */ > > + if (note->n_namesz == 4 && > > + note->n_type == NT_GNU_PROPERTY_TYPE_0 && > > + memcmp(note + 1, "GNU", 4) == 0) { > > + u8 *ptr, *ptr_end; > > + > > + /* Check for invalid property. */ > > + if (note->n_descsz < 8 || > > + (note->n_descsz % align) != 0) > > + return 0; > > + > > + /* Start and end of property array. */ > > + ptr = (u8 *)(note + 1) + 4; > > + ptr_end = ptr + note->n_descsz; > > Exploitable bug here? You haven't checked that ptr is in bounds or > that ptr + ptr_end is in bounds (or that ptr_end > ptr, for that > matter). > > > + > > + while (1) { > > + u32 type = *(u32 *)ptr; > > + u32 datasz = *(u32 *)(ptr + 4); > > + > > + ptr += 8; > > + if ((ptr + datasz) > ptr_end) > > + break; > > + > > + if (type == GNU_PROPERTY_X86_FEATURE_1_AND && > > + datasz == 4) { > > + u32 p = *(u32 *)ptr; > > + > > + if (p & GNU_PROPERTY_X86_FEATURE_1_SHSTK) > > + *shstk = 1; > > + if (p & GNU_PROPERTY_X86_FEATURE_1_IBT) > > + *ibt = 1; > > + return 1; > > + } > > + } > > + } > > + > > + /* > > + * Note sections like .note.ABI-tag and .note.gnu.build-id > > + * are aligned to 4 bytes in 64-bit ELF objects. > > + */ > > + note = (void *)note + ELF_NOTE_NEXT_OFFSET(note, align); > > A malicious value here will probably just break out of the while > statement, but it's still scary. > > > + } > > + > > + return 0; > > +} > > + > > +static int check_pt_note_segment(struct file *file, > > + unsigned long note_size, loff_t *pos, > > + u32 align, int *shstk, int *ibt) > > +{ > > + int retval; > > + char *note_buf; > > + > > + /* > > + * Try to read in the whole PT_NOTE segment. > > + */ > > + note_buf = kmalloc(note_size, GFP_KERNEL); > > kmalloc() with fully user-controlled, unchecked size is not a good idea. I will fix these problems. Thanks!