Received: by 10.192.165.148 with SMTP id m20csp4291264imm; Mon, 30 Apr 2018 15:42:05 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpPBgHgP9jCXfjqBOY7mniYXwtshX43VVq3yuR2ShHkBzChZdUplP8XF6QXL9dR7iXJbEay X-Received: by 2002:a17:902:bf0a:: with SMTP id bi10-v6mr13752856plb.235.1525128125375; Mon, 30 Apr 2018 15:42:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525128125; cv=none; d=google.com; s=arc-20160816; b=oRx9JBAmAbSnJNYat7TnWCreukVtW+oU+5R/oY9V0ncMzK7eNJmpNJdAJqdRCKPQOd LnD6+rPr0mLt/DPst0r+HNTp02gqJVv78OP8XiCohGutku7b3YXdjZDSx9etg1PSb3zU haPbZGYxthlB47Wc9ieIsawfw64jGbhmPhuYhxdaQAsw589jzyMkELLJsuIJTUSSjKUS URIO3aJ7zzy6VokvBM83cBMwXWNRhoS8TpWwGbXUgo6S/aIe5YgU8xn/4GeaF0peO5uG JDMeWTZRWTpCuBpwsAREiGDRiTbwFzq5Rzbo6FpWLCI/7NaVh/7jZCnSiw9zHR0PFeCQ 6jvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=Txo18T9LhDgzYDmwZfSCVTB68r5VIVd20+0fMS/4FAU=; b=KqPDFWPNH3V7fP+f0BkzT3UiDPLnwsME+ZHJ7DS+H0KJ5QfukC2wC4+kFBWe4MBlwm /o8tBcGAfvi3E41XR1ZqLKu7Wr0KTS/Zn20HKFz7pgGzdi7qOKYCnRbOzu+A5e3PhZio 33X7iWffHGZ17Iinfi6lTO6ZdT81aaCekEOtJHDj2kYLqGGAjGtaRsnwF044MiJ8Yk8t OEDVacE2IPQxraBLIdsXgMFBZfQ2bw5IB6rcfb04Zi20tjIM6GqCgZU3+ipqhyttqeXh FLKAWI56jQrrNj2wOxNeGg8xiNWOPlsCC8UWzUy15rOnpa+fSQL8uGm8tIO/D4TLVX85 PTMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=t8Qc6xg5; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f34-v6si8212361plf.362.2018.04.30.15.41.51; Mon, 30 Apr 2018 15:42:05 -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; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=t8Qc6xg5; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755329AbeD3Wli (ORCPT + 99 others); Mon, 30 Apr 2018 18:41:38 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:58176 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751772AbeD3Wlh (ORCPT ); Mon, 30 Apr 2018 18:41:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=Txo18T9LhDgzYDmwZfSCVTB68r5VIVd20+0fMS/4FAU=; b=t8Qc6xg539f9p2kyAUpr+aTln I7qkJpAyORuvrc1koASfPeRColxbpKjgbTz3IP1hgeOu9IONLX4SOXCXo+7eFoiBuN14Xaucar367 CL1A14rDGQaEfThbW/9x4/ewwNPO5Lf3VryiwprHWsd4REkSI8DOfjF3M7KyC+7nVVRIw/HihRW6c 9yKZBqzQsEt8IzDPIr8nag0Lz8D0CyT2ObWTDQOXLTHIdXENoS/qHK272sbezjEfgL/9kKFI5IkQP hLWRPaqdKD5HyFc6FhWKGaR0RfnmhBk4LlI585Og+ZzIkkaemwEPFu8OqFzNLlCZD8nHVFL+q472E LJYNgRXvw==; Received: from willy by bombadil.infradead.org with local (Exim 4.90_1 #2 (Red Hat Linux)) id 1fDHUD-0006ze-IC; Mon, 30 Apr 2018 22:41:33 +0000 Date: Mon, 30 Apr 2018 15:41:33 -0700 From: Matthew Wilcox To: Rasmus Villemoes Cc: Kees Cook , Julia Lawall , Andrew Morton , Matthew Wilcox , Linux-MM , LKML , Kernel Hardening , cocci@systeme.lip6.fr, Himanshu Jha Subject: Re: [PATCH 2/2] mm: Add kvmalloc_ab_c and kvzalloc_struct Message-ID: <20180430224133.GA7076@bombadil.infradead.org> References: <20180308025812.GA9082@bombadil.infradead.org> <20180308230512.GD29073@bombadil.infradead.org> <20180313183220.GA21538@bombadil.infradead.org> <20180429203023.GA11891@bombadil.infradead.org> <20180430201607.GA7041@bombadil.infradead.org> <4ad99a55-9c93-5ea1-5954-3cb6e5ba7df9@rasmusvillemoes.dk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4ad99a55-9c93-5ea1-5954-3cb6e5ba7df9@rasmusvillemoes.dk> User-Agent: Mutt/1.9.2 (2017-12-15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 30, 2018 at 11:29:04PM +0200, Rasmus Villemoes wrote: > On 2018-04-30 22:16, Matthew Wilcox wrote: > > On Mon, Apr 30, 2018 at 12:02:14PM -0700, Kees Cook wrote: > >> (I just wish C had a sensible way to catch overflow...) > > > > Every CPU I ever worked with had an "overflow" bit ... do we have a > > friend on the C standards ctte who might figure out a way to let us > > write code that checks it? > > gcc 5.1+ (I think) have the __builtin_OP_overflow checks that should > generate reasonable code. Too bad there's no completely generic > check_all_ops_in_this_expression(a+b*c+d/e, or_jump_here). Though it's > hard to define what they should be checked against - probably would > require all subexpressions (including the variables themselves) to have > the same type. Nevertheless these generate much better code than our current safeguards! extern void *malloc(unsigned long); #define ULONG_MAX (~0UL) #define SZ 8UL void *a(unsigned long a) { if ((ULONG_MAX / SZ) > a) return 0; return malloc(a * SZ); } void *b(unsigned long a) { unsigned long c; if (__builtin_mul_overflow(a, SZ, &c)) return 0; return malloc(c); } (a lot of code uses a constant '8' as sizeof(void *)). Here's the difference with gcc 7.3: 0: 48 b8 fe ff ff ff ff movabs $0x1ffffffffffffffe,%rax 7: ff ff 1f a: 48 39 c7 cmp %rax,%rdi d: 76 09 jbe 18 f: 48 c1 e7 03 shl $0x3,%rdi 13: e9 00 00 00 00 jmpq 18 14: R_X86_64_PLT32 malloc-0x4 18: 31 c0 xor %eax,%eax 1a: c3 retq vs 20: 48 89 f8 mov %rdi,%rax 23: ba 08 00 00 00 mov $0x8,%edx 28: 48 f7 e2 mul %rdx 2b: 48 89 c7 mov %rax,%rdi 2e: 70 05 jo 35 30: e9 00 00 00 00 jmpq 35 31: R_X86_64_PLT32 malloc-0x4 35: 31 c0 xor %eax,%eax 37: c3 retq We've traded a shl for a mul (because shl doesn't set Overflow, only Carry, and that's only bit 65, not an OR of bits 35-n), but we lose the movabs and cmp. I'd rather run the second code fragment than the first.