From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Google-Smtp-Source: AG47ELs9c660yUSvEo1dPF8GMIB90M0gz6iS+Xem1pcMFrHN+IE9YeKWTO509WUKCYBDlhl330Hr ARC-Seal: i=1; a=rsa-sha256; t=1521155371; cv=none; d=google.com; s=arc-20160816; b=NtduGRsmoznJNryThsEQAiqxcPRG7KM4Ud4d2XBuUhrXd8XMpfPZkLSWbQvsEgppgN 8I4kNMtdTMRxUU4w9Oy6060FBusdc5fmkKqJDvpEUNOgWz7s2OTHeRTU6F5B2GUcMKHe rcIZJaaTUiK+pJFmcHGxxtlpADFc9gMNuRry6ZxWCRaV+Dv4S6j3xHSR5pUthvYsLIoR x44v0ZcB8do7GwesV1xRICZ3m117fAAWq5gTD6UVDtERnFn1oXi6HF74gptrREZfo66z qjvXu+2mKgKiLvu+3P7YuqIkWiyZeoaBpjCoLbaDk4XBnZzLeJ/0JK/4Xcy+n4roI+mc /J9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:dkim-signature:delivered-to:list-id:list-subscribe :list-unsubscribe:list-help:list-post:precedence:mailing-list :arc-authentication-results; bh=ktfWGoiSVcoqMdiqi52AwGfP5SWzrwWK2cNVLGBjaAA=; b=LV+MpxJhOy7+MJZHNmt4ZwZRI9pRPra+fTkWcFqCSXgZ7a0zdSrpH1hln0Olx3poNG +P7ZQqYJZLF1+UePbZxsrr341j8NJZvbkLJDLKSbIX6aUDViyv7g9P3hPCR+ZcPVu0Vd a6m0XciJFkN2y0OblqkdTaj8XTmG64OU2R/85ZT3zhu7q0TDdqTUxME3gk002hI6LgKy ALurDV7H9HiNsrNnuCpPvIN/exANZ8t3siu8T+eD6ETqh8FlR6MxnHES3ZrUoVSRGeLf fDvkEDWmnfzYj1BQTfoD5GUnmDEc7K5NfnDXtYtYS+k/2ARLEufA455QMfXWDhodsbK4 jvkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=RdK+s1WL; spf=pass (google.com: domain of kernel-hardening-return-12649-gregkh=linuxfoundation.org@lists.openwall.com designates 195.42.179.200 as permitted sender) smtp.mailfrom=kernel-hardening-return-12649-gregkh=linuxfoundation.org@lists.openwall.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=RdK+s1WL; spf=pass (google.com: domain of kernel-hardening-return-12649-gregkh=linuxfoundation.org@lists.openwall.com designates 195.42.179.200 as permitted sender) smtp.mailfrom=kernel-hardening-return-12649-gregkh=linuxfoundation.org@lists.openwall.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm List-Post: List-Help: List-Unsubscribe: List-Subscribe: MIME-Version: 1.0 In-Reply-To: References: <1521143266-31350-1-git-send-email-keescook@chromium.org> <1521143266-31350-2-git-send-email-keescook@chromium.org> From: Miguel Ojeda Date: Fri, 16 Mar 2018 00:08:49 +0100 Message-ID: Subject: Re: [PATCH v4 1/2] kernel.h: Introduce const_max() for VLA removal To: Kees Cook Cc: Linus Torvalds , Andrew Morton , Josh Poimboeuf , Rasmus Villemoes , Randy Dunlap , Ingo Molnar , David Laight , Ian Abbott , linux-input , linux-btrfs , Network Development , Linux Kernel Mailing List , Kernel Hardening Content-Type: text/plain; charset="UTF-8" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: =?utf-8?q?1595034361293703830?= X-GMAIL-MSGID: =?utf-8?q?1595047014586974197?= X-Mailing-List: linux-kernel@vger.kernel.org List-ID: On Thu, Mar 15, 2018 at 11:58 PM, Miguel Ojeda wrote: > On Thu, Mar 15, 2018 at 11:46 PM, Kees Cook wrote: >> >> By using this eye-bleed: >> >> size_t __error_not_const_arg(void) \ >> __compiletime_error("const_max() used with non-compile-time constant arg"); >> size_t __error_not_positive_arg(void) \ >> __compiletime_error("const_max() used with negative arg"); >> #define const_max(x, y) \ >> __builtin_choose_expr(__builtin_constant_p(x) && \ >> __builtin_constant_p(y), \ >> __builtin_choose_expr((x) >= 0 && (y) >= 0, \ >> (typeof(x))(x) > (typeof(y))(y) ? \ >> (x) : (y), \ >> __error_not_positive_arg()), \ >> __error_not_const_arg()) >> > > I was writing it like this: > > #define const_max(a, b) \ > ({ \ > if ((a) < 0) \ > __const_max_called_with_negative_value(); \ > if ((b) < 0) \ > __const_max_called_with_negative_value(); \ > if (!__builtin_types_compatible_p(typeof(a), typeof(b))) \ > __const_max_called_with_incompatible_types(); \ > __builtin_choose_expr((a) > (b), (a), (b)); \ > }) The full one, using your naming convention: #define const_max(x, y) \ ({ \ if (!__builtin_constant_p(x)) \ __error_not_const_arg(); \ if (!__builtin_constant_p(y)) \ __error_not_const_arg(); \ if (!__builtin_types_compatible_p(typeof(x), typeof(y))) \ __error_incompatible_types(); \ if ((x) < 0) \ __error_not_positive_arg(); \ if ((y) < 0) \ __error_not_positive_arg(); \ __builtin_choose_expr((x) > (y), (x), (y)); \ }) Miguel