Received: by 10.192.165.148 with SMTP id m20csp184430imm; Thu, 3 May 2018 17:42:11 -0700 (PDT) X-Google-Smtp-Source: AB8JxZoaEIq9/Od1w3tadGGyZyg/N6Xart69o01TleUygdL9wtB5KQ3kwqr1ocwkWysr4SJ4GiuZ X-Received: by 2002:a17:902:8307:: with SMTP id bd7-v6mr25453599plb.234.1525394531756; Thu, 03 May 2018 17:42:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525394531; cv=none; d=google.com; s=arc-20160816; b=JTeQ+gxJ53C2Wmk/phWa5UAM6M+zigvUF2X9quyzAPbA1XHwMLFUryKsJlxFhXAVrY qDtvxrLpIx159HbWYB7en/8GnrFckyV1h1Xdan3GIiT88ptLY4YXqOtEnokcTpSGmucn FOjiPXgAnousB1PCb1lc5YfnF1/sssxwR/Gpek8jwKBbHRwKyOTGurRM4jTHEGXAkB+Z ndVptT/P2XlmHUECo8k/ZxbZqSE9uKxADqFNejgpfucDcCAwBin0KW0ZHfoXBi+ghkFg X3RmYHlwxPioZWEMUg50TiZ4hSPK9W14eYLRoF7nsbPxbYH+p8eGlUW93gYFfg0IYrZM kdhw== 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=Vj/HXFdev9nhJmWQrtVw+nZu9RDoSR+a4jXTEdWDIY0=; b=eVzGnZsAuoVl9PL70A00kuximOR2RzX1z792DUcYBmiM7agQFa3wg2NQF+39VFaWtp OrWrCh3hXIZ762eWS2hKV0ZaMaOPbRqSIdskRyb3RvWqfBRLzMGSmahXMOGAYbclThF8 BpUh0giNQDRiaSmufaY9vK/Iel67QuWTfAudME9M300A4gD9FEKYjZgUewkOkKfPVMy7 RFCaqIZjzMu/X7WqEqG+nZdldkl5cO2154/APSe0JiSeKs+QPkU09SUAPFv6/qGEX2/W XI0H/3KZOLLA17rWy9D+Prj26LGLr6IVi4DAZfVHHyd4c+0JrYDdCrs+QKi4/tXwYhA1 CW8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@google.com header.s=20161025 header.b=LLsU9Otp; dkim=fail header.i=@chromium.org header.s=google header.b=VGUx2KCj; 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 s13-v6si2003943plp.350.2018.05.03.17.41.57; Thu, 03 May 2018 17:42: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=LLsU9Otp; dkim=fail header.i=@chromium.org header.s=google header.b=VGUx2KCj; 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 S1751236AbeEDAkh (ORCPT + 99 others); Thu, 3 May 2018 20:40:37 -0400 Received: from mail-ua0-f196.google.com ([209.85.217.196]:40661 "EHLO mail-ua0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750965AbeEDAkg (ORCPT ); Thu, 3 May 2018 20:40:36 -0400 Received: by mail-ua0-f196.google.com with SMTP id g9so13008795uak.7 for ; Thu, 03 May 2018 17:40:35 -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=Vj/HXFdev9nhJmWQrtVw+nZu9RDoSR+a4jXTEdWDIY0=; b=LLsU9OtpWWDQBSuFzjlZyheCMjhgO8BVRQJxbxEdMKlgV6DQMByPTXwpOsoMAgqiif CU4gwr5IwIh6gf+g4Q5CaSyHH10aeV/3ntEuijdiv7Kjm2yAokoQWOBvUZvlk4YSc+o5 eAY0zWoiZR+UQgx0RoQJznRQNy8WrrL1AS9zsykFMZ+HV7Z7F3ADnHQj802dgxZHvU7f 8PzV93nbu3W3qr/NFFx9LtnsUTF9mBHnHC6ViiBzRWIhxv4QJzbUMxC3ZOw19a1TsoO6 pDq/Clm9GPZhVkSMZ7qJhYg0/afQGkWh2SH9yqAyHfu/MxnqYT4+naSGcDT4KaD8rTDC jUsg== 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=Vj/HXFdev9nhJmWQrtVw+nZu9RDoSR+a4jXTEdWDIY0=; b=VGUx2KCjS9Zlog+hQrVLGBarCY54Y/MyR3IvOJUhokjJ/4JS1zMr9Y3DAMQh6pMdaV qYKB+voFOG72eA26jOBu4ucya9hIbwxFED6T+ACk14iWMtTC5C6rjrBh8Zh7Af6XRfM6 APXoWr3BTd3viiJ0T+O4NbT5wLSb13DKpj2po= 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=Vj/HXFdev9nhJmWQrtVw+nZu9RDoSR+a4jXTEdWDIY0=; b=S1ETYfj2Np9WBZszwZ5n3W+2QDwufg9c0ACtV8Ci0MK98FQaAHhMoZNDs3FzAYbhml yvNmWkaRvpWZrs0UPtSSA2/O8n7+okmDx5mySBH+pORcly8XP8RVEQv6gFC39Tk2VGQF tpTc5DxQZks/8CcPYZmXbM+1IamMvtdYsol3Qxcu3ufGxghJkRHSuoY2ioLq6WFlpMHj Vu00fYISHYesd6OVYdjGSmpS+tIEtFQNaZznQlGu8sxc5DH7MreoLiUVQRGBOn2niF9c yK+SJJUvtQdrJeugXswPT66A8rpYaHdU0VnyjLwPjhQVY2+sG7fN6A4Vk8h410qY0osj n4pA== X-Gm-Message-State: ALQs6tBErpvDPIe5aXkpWyL3mDSzQ50NVawVYCwT0EPFCF+Wby0gVs2G 6xgnHrhFcI90MrBgEfLL9kYx6beTnbRlyfwstcbLAQ== X-Received: by 10.176.76.226 with SMTP id e34mr23538063uag.0.1525394435348; Thu, 03 May 2018 17:40:35 -0700 (PDT) MIME-Version: 1.0 Received: by 10.31.11.209 with HTTP; Thu, 3 May 2018 17:40:34 -0700 (PDT) In-Reply-To: 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> <4e25ff5b-f8fc-7012-83c2-b56e6928e8bc@rasmusvillemoes.dk> From: Kees Cook Date: Thu, 3 May 2018 17:40:34 -0700 X-Google-Sender-Auth: l6jFswksGn4nB7Rxa36nH4FVUbo Message-ID: Subject: Re: [PATCH 2/2] mm: Add kvmalloc_ab_c and kvzalloc_struct To: Rasmus Villemoes Cc: Daniel Vetter , Matthew Wilcox , Julia Lawall , Andrew Morton , Matthew Wilcox , Linux-MM , LKML , Kernel Hardening , cocci@systeme.lip6.fr, Himanshu Jha , Linus Torvalds 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 Thu, May 3, 2018 at 5:36 PM, Kees Cook wrote: > On Thu, May 3, 2018 at 4:00 PM, Rasmus Villemoes > wrote: >> On 2018-05-01 19:00, Kees Cook wrote: >>> On Mon, Apr 30, 2018 at 2:29 PM, Rasmus Villemoes >>> wrote: >>>> >>>> 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. >>>> >>>> plug: https://lkml.org/lkml/2015/7/19/358 >>> >>> That's a very nice series. Why did it never get taken? >> >> Well, nobody seemed particularly interested, and then >> https://lkml.org/lkml/2015/10/28/215 happened... but he did later seem >> to admit that it could be useful for the multiplication checking, and >> that "the gcc interface for multiplication overflow is fine". > > Oh, excellent. Thank you for that pointer! That conversation covered a > lot of ground. I need to think a little more about how to apply the > thoughts there with the kmalloc() needs and the GPU driver needs... > >> I still think even for unsigned types overflow checking can be subtle. E.g. >> >> u32 somevar; >> >> if (somevar + sizeof(foo) < somevar) >> return -EOVERFLOW; >> somevar += sizeof(this); >> >> is broken, because the LHS is promoted to unsigned long/size_t, then so >> is the RHS for the comparison, and the comparison is thus always false >> (on 64bit). It gets worse if the two types are more "opaque", and in any >> case it's not always easy to verify at a glance that the types are the >> same, or at least that the expression of the widest type is on the RHS. > > That's an excellent example, yes. (And likely worth including in the > commit log somewhere.) > >> >>> It seems to do the right things quite correctly. >> >> Yes, I wouldn't suggest it without the test module verifying corner >> cases, and checking it has the same semantics whether used with old or >> new gcc. >> >> Would you shepherd it through if I updated the patches and resent? > > Yes, though we may need reworking if we actually want to do the > try/catch style (since that was talked about with GPU stuff too...) > > Either way, yes, a refresh would be lovely! :) Whatever the case, I think we need to clean up all the kmalloc() math anyway. As mentioned earlier, there are a handful of more complex cases, but the vast majority are just A * B. I've put up a series here now, and I'll send it out soon. I want to think more about 3-factor products, addition, etc: https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/log/?h=kspp/kmalloc/2-factor-products The commit logs need more details (i.e. about making constants the second argument for optimal compiler results, etc), but there's a Coccinelle-generated first pass. -Kees -- Kees Cook Pixel Security