Received: by 10.192.165.148 with SMTP id m20csp4283934imm; Mon, 30 Apr 2018 15:31:11 -0700 (PDT) X-Google-Smtp-Source: AB8JxZo5SuMnh3t+8DNwUMvTfTn1/fXaDK9w6Iw6NfvT8g5UuA5qNofRuBzE1om4vM8v1rhqJO+B X-Received: by 2002:a63:7f17:: with SMTP id a23-v6mr10890317pgd.99.1525127471591; Mon, 30 Apr 2018 15:31:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525127471; cv=none; d=google.com; s=arc-20160816; b=N0+im0A5yOOWnHaQa0B/fGqFcihUP7MX4Q1mdqztW9YGd0sRRqP2LNzinarogRmfBx yAz9MPYXEYZIXNk9uap2k/SEggzKCvvlNJpdGuVMx36r0ftDOZaFXWBFstkVoGbWHNw9 0bg1W21S0TzKhcX19anVNSif+PmEf+XuxDuSwXTKERm2ue+yUfib8CbBQPM1VHnH5Sc2 7FaoDClFpgSPr02rG2KTMk/7KnO3tF8MlYK58ZtqqtqTIKNKylhviSg/iVPMgXtZErji Y4fW2x7ZCebROdYgwuCtPH33KpnHF1ByJOBn3ispHFREo6n9WPvji5nV6m65xdMgwkzL rljQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature:dkim-signature :arc-authentication-results; bh=XYsiQ3ReayoTPLTrDMb2QzuFbPxgsfTQS7sJv7z8o7c=; b=wE/WeX/KNPDNetuHDtSnsgkcMkkYVGrr3SzXCldEmdrXLldvtUVjEEtZAUqnYk4KMf vlsOIIHj2zuHPGqnXDafKnS0V3mKgRhYaMeBNVAxB3rYA0czSFYX7iv2ycmo3Y1FDsHh FFUxw2T6XhAMUkYLTiiFtFuxvDCc895cQwrfMxMZSPXPB2sFoaTmq6pHOEDgdXPCKJae JZSFrcJBr6CTR5fiZYAXiT1LP7nRhURaGZ+IKrLFZnbVNuECvVTFK9LmxyE+RHdVEAbv A2NN8QELY94FOBCmA5A8on5c2qNsupu1ixegKUF8XIZlRp6OoBhrKhw549iSuTinVpn3 aI2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@google.com header.s=20161025 header.b=olZGv/u+; dkim=fail header.i=@chromium.org header.s=google header.b=WdKjFw//; 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=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i190-v6si6987513pge.408.2018.04.30.15.30.57; Mon, 30 Apr 2018 15:31:11 -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=@google.com header.s=20161025 header.b=olZGv/u+; dkim=fail header.i=@chromium.org header.s=google header.b=WdKjFw//; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755147AbeD3W3m (ORCPT + 99 others); Mon, 30 Apr 2018 18:29:42 -0400 Received: from mail-vk0-f46.google.com ([209.85.213.46]:37423 "EHLO mail-vk0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753530AbeD3W3k (ORCPT ); Mon, 30 Apr 2018 18:29:40 -0400 Received: by mail-vk0-f46.google.com with SMTP id m144-v6so6086161vke.4 for ; Mon, 30 Apr 2018 15:29:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=XYsiQ3ReayoTPLTrDMb2QzuFbPxgsfTQS7sJv7z8o7c=; b=olZGv/u+9NnYJMj/5l7pJQn9DA00sZL4ZzVzKK2jFP90MKHDMPu4Y9Mb8ztjixlRgT PmQ30OyG7MJFL0gYa7G9Vd6S/7hIOLzkn/L4xWXWhmQGT0hT57Sy8KYta2RWfp9C6iwI UYIphOVpoPgK09nZbLO7GX7e8kFrZTswSQPSZGwrchguxtsOM0gWwwj4Y2chAycfBxjT ioexghR2KkxB2H4Ho+39uw4aUWDuQ65xkhc5toOTm1AJIoXWDFv60gA/dWl9aeiRHgSY Y6NhYjq9rCXp2OO7xf+1MFPoEVUa48/grZ1/xjBJChUV4WqHS0+rlZSHBLEZt1PpReju uaRg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=XYsiQ3ReayoTPLTrDMb2QzuFbPxgsfTQS7sJv7z8o7c=; b=WdKjFw//wp8wFVz5FCTq07WbNKXcItpbmKJOErE3Kt2KgR9UJEbz5lTgfzkL4/B3pY pOWtzZp3xOsslJ9jQ2Al7we19nb31ln41LIkv5R5x+6UmcDWtYfGLWqf27zyUoRbRKLR R3bhw7B1v9FuO5ypkxBiYvXsmlP7C3HoHs8NI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=XYsiQ3ReayoTPLTrDMb2QzuFbPxgsfTQS7sJv7z8o7c=; b=RkvHgJvMp7aSZg5lmLmU7QQGwLBUHWdMaFD+FJGKsqyC24EMYHzLMip+Uw/nUwExiH I+f4NZhToIIdjEif8p2hSctA0tf9Jb/PjJglNZKvPpLJFZ9RfWJtDxpmTBm8ucJ5KfVn pYM9lZFrB11BHekXrgIFyQ+JCWGsPEDw7AxBXHJGeR/o0ixRbgSTlZrGZHfhv2qItsEi 2IKXc/6BmQ0hWLmuI6f9E9R0vayNF86wFYKbluLGlbCUtbXLPPf66yI59s59xYO4HgoB jWWgHLyrL94CO8fcc6bYx8URk9sZQD7BZRCpIPYDcoD1ahJ4WYY4PrknBCHWOZ8o7CRo SLOw== X-Gm-Message-State: ALQs6tChIDOeQDXhnFvQw1LyqRIpsdVzzc/WwdC1HXLnZsng2iQK88ug 5C7SQhDgJKIC90u1IvK8NGhfH0XjJkZvwUvQIzxS5A== X-Received: by 2002:a1f:b7c6:: with SMTP id h189-v6mr8343893vkf.84.1525127379701; Mon, 30 Apr 2018 15:29:39 -0700 (PDT) MIME-Version: 1.0 Received: by 10.31.11.209 with HTTP; Mon, 30 Apr 2018 15:29:39 -0700 (PDT) In-Reply-To: <20180430201607.GA7041@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> From: Kees Cook Date: Mon, 30 Apr 2018 15:29:39 -0700 X-Google-Sender-Auth: _xKXnbL4jcSvxlNCCSxCv4GnNdM Message-ID: Subject: Re: [PATCH 2/2] mm: Add kvmalloc_ab_c and kvzalloc_struct To: Matthew Wilcox Cc: Julia Lawall , Andrew Morton , Matthew Wilcox , Linux-MM , LKML , Kernel Hardening , cocci@systeme.lip6.fr, Himanshu Jha Content-Type: text/plain; charset="UTF-8" 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 1:16 PM, Matthew Wilcox wrote: > On Mon, Apr 30, 2018 at 12:02:14PM -0700, Kees Cook wrote: >> For any longer multiplications, I've only found[1]: >> >> drivers/staging/rtl8188eu/os_dep/osdep_service.c: void **a = >> kzalloc(h * sizeof(void *) + h * w * size, GFP_KERNEL); > > That's pretty good, although it's just an atrocious vendor driver and > it turns out all of those things are constants, and it'd be far better > off with just declaring an array. I bet they used to declare one on > the stack ... Yeah, it was just a quick hack to look for stuff. > >> At the end of the day, though, I don't really like having all these >> different names... >> >> kmalloc(), kmalloc_array(), kmalloc_ab_c(), kmalloc_array_3d() >> >> with their "matching" zeroing function: >> >> kzalloc(), kcalloc(), kzalloc_ab_c(), kmalloc_array_3d(..., gfp | __GFP_ZERO) > > Yes, it's not very regular. > >> For the multiplication cases, I wonder if we could just have: >> >> kmalloc_multN(gfp, a, b, c, ...) >> kzalloc_multN(gfp, a, b, c, ...) >> >> and we can replace all kcalloc() users with kzalloc_mult2(), all >> kmalloc_array() users with kmalloc_mult2(), the abc uses with >> kmalloc_mult3(). > > I'm reluctant to do away with kcalloc() as it has the obvious heritage > from user-space calloc() with the addition of GFP flags. But it encourages misuse with calloc(N * M, gfp) ... if we removed calloc and kept k[mz]alloc_something(gfp, a, b, c...) I think we'd have better adoption. >> That said, I *do* like kmalloc_struct() as it's a very common pattern... > > Thanks! And way harder to misuse than kmalloc_ab_c(). Yes, quite so. It's really why I went with kmalloc_array_3d(), but now I'm thinking better of it... >> Or maybe, just leave the pattern in the name? kmalloc_ab(), >> kmalloc_abc(), kmalloc_ab_c(), kmalloc_ab_cd() ? >> >> Getting the constant ordering right could be part of the macro >> definition, maybe? i.e.: >> >> static inline void *kmalloc_ab(size_t a, size_t b, gfp_t flags) >> { >> if (__builtin_constant_p(a) && a != 0 && \ >> b > SIZE_MAX / a) >> return NULL; >> else if (__builtin_constant_p(b) && b != 0 && \ >> a > SIZE_MAX / b) >> return NULL; >> >> return kmalloc(a * b, flags); >> } > > Ooh, if neither a nor b is constant, it just didn't do a check ;-( This > stuff is hard. Yup, quite true. Obviously not the final form. ;) I meant to illustrate that we could do compile-time tricks to reorder the division in an efficient manner. >> (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? On the CPU it's not retained across multiple calculations. And the type matters too. This came up recently in a separate thread too: http://openwall.com/lists/kernel-hardening/2018/03/26/4 >> [1] git grep -E 'alloc\([^,]+[^(]\*[^)][^,]+[^(]\*[^)][^,]+[^(]\*[^)][^,]+,' > > I'm impressed, but it's not going to catch > > veryLongPointerNameThatsMeaningfulToMe = kmalloc(initialSize + > numberOfEntries * entrySize + someOtherThing * yourMum, > GFP_KERNEL); Right, it wasn't meant to be exhaustive. I just included it in case anyone wanted to go grepping around for themselves. -Kees -- Kees Cook Pixel Security