Received: by 10.192.165.148 with SMTP id m20csp203134imm; Wed, 9 May 2018 11:09:17 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqPlw4vGNwlueeML/zbobeRn9Yvli+1IE6b9A0NX6bcPhfdoRWQiVJV8TBb49dWByTDO3vG X-Received: by 10.98.15.200 with SMTP id 69mr36453232pfp.14.1525889357164; Wed, 09 May 2018 11:09:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525889357; cv=none; d=google.com; s=arc-20160816; b=RKUcMG1L2CwFCyU0TWoJLHkymrCQFGywe9AyXldoGcz8w+vJjjlc6VapGmE04oPtd7 T+AAc3Yc8NaqWqkKxk+TZ+geJ0gBVOMDSpKLtgdjCSFSeWm5QScPhZUb+FZXA4WTW454 WsIqHIL2J1dNBTgiTZO0V5RoYXj7bFbm9Vsz6YnrjBqi9GkWcBZEhmAHzzt5NCUU9Udi PQ7eMaObt207aIOhDH/KHGj88Eq4b5xyxJylIXo6NvUGmcZJcWawQfZOzFtGxcK4hwmA t8xdawKe/KmRk9EqnNCMI68h598iJ98173dDhoRC9q3bA2qqO1grZTD7MYNZBnCNlTti dp3g== 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=N30Mslw6jZ6+2vjBhQE7gmYtpzFbiVMXZN2Ob36wsZQ=; b=ywafdxAnbIUj8tmibAkrGiBW0oCH87F2YCgIsv49dLSubkVwsAGzn7zdRW0daHDp5A ogh/gwR397FwRCu+bpOw65XujNHV3F7TRL8vMfMzWLZiJWRBlOXlQDOfSe43/3tIDb+o PNn2Ad4cstehIhqPQm4oC3mC0iODPxHVXywirzpCaijRXoO8g0iOi3vsF5GbEGDRYDtU 37ePTNV6Jjsu/V0cuyzZJdJXtYLXJTUEtY7bAkQXsnjcmCr+KGopc2xcRKf3f6Ew+bCv T3mtTbb9QScIdtNk53kuWWD4uxCh8EgvBgdvesTjYPOs+wkqkc2J6b8FNrHSOIIgPLb5 wPbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@google.com header.s=20161025 header.b=t+ux11f2; dkim=fail header.i=@chromium.org header.s=google header.b=CK9s2sH2; 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 l5-v6si10365584pgp.239.2018.05.09.11.09.02; Wed, 09 May 2018 11:09:17 -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=t+ux11f2; dkim=fail header.i=@chromium.org header.s=google header.b=CK9s2sH2; 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 S935179AbeEISHX (ORCPT + 99 others); Wed, 9 May 2018 14:07:23 -0400 Received: from mail-ua0-f196.google.com ([209.85.217.196]:46832 "EHLO mail-ua0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933490AbeEISHV (ORCPT ); Wed, 9 May 2018 14:07:21 -0400 Received: by mail-ua0-f196.google.com with SMTP id e8so21339019uam.13 for ; Wed, 09 May 2018 11:07:21 -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=N30Mslw6jZ6+2vjBhQE7gmYtpzFbiVMXZN2Ob36wsZQ=; b=t+ux11f21X7RD7j6+1en8cSSdmPQj05CERHTu2O/p3jt7zmt8F0WkpVHzSkEUGeqI3 Cu8BKHInvRFj/TLsnZlAv3AQ3u9bhrotajcCb7FA+ucnmrvN9YYSeRT/r+E0oMjb8Ht+ MlCX8q1phEB65HEmt2wrQ30NjBX30sPIG/lXK/p0eDN+A/JHt4/6eDf6+qFEb4J01U4U OErCMJncdmqAh/1nSOGtACpktVVV40ZnODDXLAmnumQVaTvj7/fB5nfnz8HxfQ2f+Q6I pZUw8wQjZN8Kk6dS3bAdb0eOyHJmM8o0+4MhxP1lLTlfsDIgpjmnXNbzGRomdmFsaDKx O+lg== 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=N30Mslw6jZ6+2vjBhQE7gmYtpzFbiVMXZN2Ob36wsZQ=; b=CK9s2sH28E9mFoT4lwwU2VQqOCeiU8o+3K9Ma5EqzC6rYOUZqcPdc0zlp2iiq/jAl1 9yupVxQPVNvpKqemUcXhdM1qzNiR6Vg94q7wIZuCtf/0T8kNPvgfHgC+U2fcSFL4O1rb CGFGT54HmQlMaIjEpWjBtyTYNFrH6D0A2owj4= 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=N30Mslw6jZ6+2vjBhQE7gmYtpzFbiVMXZN2Ob36wsZQ=; b=hzfCrkfp8DUL5h92noZZtW4abW0lGMy2ErXiC6Oq/4/OdBLNwuu+Gt67EnuVjzuLnv cZwYXZBVlOmbWWNKtYKKoMkS3AXu+CjeJvwf156ZUiC5DgbShiqCdjCYUo+th7luI0zQ hioCq6XSk2dGWDh8OuYl67f9Xu9RcsBQheXLnFUcENJXtY7Ft9KwEaj0wD8mbpoEhHaM 5MNtG+ZSJZKAofevnk0AwqlHTtAfVUZXxXIVWDrP3R8AdJw4WBbzObcYuz8ogHkNpGea GqVybGhVxZX8wUokR2p37DMpOKF5ojGvj53F22+LwSi+7noVFrki+0sZtWHhNOnaH/yC G5Ag== X-Gm-Message-State: ALQs6tBZBxYVqwCyHyXxLfez8FYo917hsqnto+6W5Wx2tYvu9QUuSD77 Y5rOpCyloa8+nQ/SMraZow3uTrFZVDOYDMdiTBSst0+E X-Received: by 10.176.84.78 with SMTP id o14mr38798739uaa.164.1525889240921; Wed, 09 May 2018 11:07:20 -0700 (PDT) MIME-Version: 1.0 Received: by 10.31.11.209 with HTTP; Wed, 9 May 2018 11:07:20 -0700 (PDT) In-Reply-To: References: <20180509004229.36341-1-keescook@chromium.org> <20180509004229.36341-5-keescook@chromium.org> <20180509113446.GA18549@bombadil.infradead.org> From: Kees Cook Date: Wed, 9 May 2018 11:07:20 -0700 X-Google-Sender-Auth: j5OPLfJOXdsPPg5OS_BYXIn08-g Message-ID: Subject: Re: [PATCH 04/13] mm: Use array_size() helpers for kmalloc() To: Rasmus Villemoes Cc: Matthew Wilcox , Matthew Wilcox , LKML , Linux-MM , Kernel Hardening 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 Wed, May 9, 2018 at 11:00 AM, Rasmus Villemoes wrote: > On 2018-05-09 13:34, Matthew Wilcox wrote: >> On Tue, May 08, 2018 at 05:42:20PM -0700, Kees Cook wrote: >>> @@ -499,6 +500,8 @@ static __always_inline void *kmalloc_large(size_t size, gfp_t flags) >>> */ >>> static __always_inline void *kmalloc(size_t size, gfp_t flags) >>> { >>> + if (size == SIZE_MAX) >>> + return NULL; >>> if (__builtin_constant_p(size)) { >>> if (size > KMALLOC_MAX_CACHE_SIZE) >>> return kmalloc_large(size, flags); >> >> I don't like the add-checking-to-every-call-site part of this patch. >> Fine, the compiler will optimise it away if it can calculate it at compile >> time, but there are a lot of situations where it can't. You aren't >> adding any safety by doing this; trying to allocate SIZE_MAX bytes is >> guaranteed to fail, and it doesn't need to fail quickly. > > Yeah, agree that we don't want to add a size check to all callers, > including those where the size doesn't even come from one of the new > *_size helpers; that just adds bloat. It's true that the overflow case > does not have to fail quickly, but I was worried that the saturating > helpers would end up making gcc emit a cmov instruction, thus stalling > the regular path. But it seems that it actually ends up doing a forward > jump, sets %rdi to SIZE_MAX, then jumps back to the call of __kmalloc, > so it should be ok. Okay, consensus is to remove new SIZE_MAX checks, then? > With __builtin_constant_p(size) && size == SIZE_MAX, gcc could be smart > enough to elide those two instructions and have the jo go directly to > the caller's error handling, but at least gcc 5.4 doesn't seem to be > that smart. So let's just omit that part for now. > > But in case of the kmalloc_array functions, with a direct call of > __builtin_mul_overflow(), gcc does combine the "return NULL" with the > callers error handling, thus avoiding the six byte "%rdi = -1; jmp > back;" thunk. That, along with the churn factor, might be an argument > for leaving the current callers of *_array alone. But if we are going to > keep those longer-term, we might as well convert kmalloc(a, b) into > kmalloc_array(a, b) instead of kmalloc(array_size(a, b)). In any case, I > do see the usefulness of the struct_size helper, and agree that we > definitely should not introduce a new *_struct variant that needs to be > implemented in all families. I'd like to drop *calloc() and *_array() to simplify APIs (and improve developer sanity). Are you suggesting we should not use the overflow helpers in kmalloc_array(), instead leaving the existing open-coded overflow check? -Kees -- Kees Cook Pixel Security