Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp479757rdb; Tue, 23 Jan 2024 05:46:14 -0800 (PST) X-Google-Smtp-Source: AGHT+IHeu1dnjmcIFy9D2sssc+znkIWxhkYMa/Wses+EeOzynf+pVyBw9FMLOCu+WGnx0KuKSLAy X-Received: by 2002:aa7:df8d:0:b0:55a:ea3:c12a with SMTP id b13-20020aa7df8d000000b0055a0ea3c12amr866492edy.20.1706017573813; Tue, 23 Jan 2024 05:46:13 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706017573; cv=pass; d=google.com; s=arc-20160816; b=yi5lKS8Awsg30fSLTdE5iSnkv8dccNPm9u5N2EeOx3F7eeF/feZ2Te5TCCAg/ywxDc 3+MNGkiiTeKTkt3LUL6jJMNHMrMvM3HWWMiGJ4w889rvn0wP1BSzASzIk/LLtxmtV6k9 Q2nt7Epu6jGHmz3WGs+nNgw/XZM9FG+iLHpf7xAE5T5U6GxT3TyQedvtH3SBlf1BNx0g gJcszCdX/ZZJbj6gZSwjvIPpB/W4uFqxZViPJRIwfR0jDOVeXsG9+cBEIEAL4h9beIbU 71/mJ26GlqzYHuuR3+lTG79ONWU/NJXfpWF4EbaNg/5GBYzkl0RtUk4b9wF04FPeUeVQ EFEg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=6NCLOzp/OB80J9Wo03J64c3yZ3YzkrG5Vmygseob59k=; fh=JEpsEMTTmXHvHK/A6KXd5GW9IVJII8kcOLEur7KCpfg=; b=iDbFM34q8x4Nleq4Z3DKp8frLzLurI3+FznL40r8BThShdEVi1KwSzgBbpVz5fX/v6 l/uxY++PgLVjhPl89kWS97HgnMbUd/u4UIPMqfuLWBUXoFu/6far7bcFgub26n2jAFL1 q1fTkmBI8so9nqnDdwwdXVWMXdcxiyeCD+GUERm8v5oSA6BO7YxoulZLhDEKLbR0Th8q +Nj4lVcAeMeRFAgKGTtUyVF3wvL+qO4TQfFeVVRRjexdjy142l19OUFg32lVGL4Hz/OC NzzGnl1vKMDOBiBjrfwZemC3Yj28h+AXCNP/EzVwtCm5B2jtjkfF4phoqoF3qQo7fIwp AJRA== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=helsinkinet.fi); spf=pass (google.com: domain of linux-kernel+bounces-35390-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35390-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id u18-20020a509512000000b0055a68f50164si4616615eda.563.2024.01.23.05.46.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 05:46:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-35390-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=helsinkinet.fi); spf=pass (google.com: domain of linux-kernel+bounces-35390-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35390-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id DC4BE1F22474 for ; Tue, 23 Jan 2024 13:36:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8C7F25EE9A; Tue, 23 Jan 2024 13:36:00 +0000 (UTC) Received: from welho-filter1.welho.com (welho-filter1b.welho.com [83.102.41.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C1BAD5F546; Tue, 23 Jan 2024 13:35:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=83.102.41.27 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706016960; cv=none; b=BhvlyFu6FvCsslPFWjS+DpMMkiHdnJoF8Glr4kqaetpjXxOWMzhLR430Rv1vCwBnA5bL5HH8pjSU8dY2CZLJs2RbK09HVejm1Xln2axCqOxk3SUN9OYCTJZ8JYvPPYfYu7Cfu6LTOIpK/1qLBnNEmiM81CwfiKB7ltqr4E3bbs0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706016960; c=relaxed/simple; bh=cFBQlzkCg1NzV3wANQZ9Afl/LsvBY/B9nxsgzPXXHP8=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=ueSOXqnzpZS+7oWcR5cdoI/N9D0L4zP6/2TcwSBzmsR9Ffsu6FMrHud0XzWQJ+Rrk88+P8ghR7ePytb3Is59uSFBlXxN7IejHlPApsTjnCN0T6xv9wbq2TrHuIoWu9ePysSWrGW6hUWgLozKlFxa3DQtgCDoBG9zLj0bYd8UAOI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=helsinkinet.fi; spf=pass smtp.mailfrom=helsinkinet.fi; arc=none smtp.client-ip=83.102.41.27 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=helsinkinet.fi Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=helsinkinet.fi Received: from localhost (localhost [127.0.0.1]) by welho-filter1.welho.com (Postfix) with ESMTP id 3A5751EB3A; Tue, 23 Jan 2024 15:30:10 +0200 (EET) X-Virus-Scanned: Debian amavisd-new at pp.htv.fi Received: from welho-smtp3.welho.com ([IPv6:::ffff:83.102.41.86]) by localhost (welho-filter1.welho.com [::ffff:83.102.41.23]) (amavisd-new, port 10024) with ESMTP id 6wcG2ajQJusf; Tue, 23 Jan 2024 15:30:09 +0200 (EET) Received: from [192.168.1.105] (82-181-33-216.bb.dnainternet.fi [82.181.33.216]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by welho-smtp3.welho.com (Postfix) with ESMTPSA id C26D62309; Tue, 23 Jan 2024 15:29:55 +0200 (EET) Message-ID: Date: Tue, 23 Jan 2024 15:29:55 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 27/82] m68k: Refactor intentional wrap-around calculation Content-Language: en-US To: Geert Uytterhoeven , Kees Cook Cc: linux-hardening@vger.kernel.org, Andrew Morton , Arnd Bergmann , Liam Howlett , "Matthew Wilcox (Oracle)" , Hugh Dickins , linux-m68k@lists.linux-m68k.org, "Gustavo A. R. Silva" , Bill Wendling , Justin Stitt , linux-kernel@vger.kernel.org References: <20240122235208.work.748-kees@kernel.org> <20240123002814.1396804-27-keescook@chromium.org> From: Eero Tamminen In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi, On 23.1.2024 10.13, Geert Uytterhoeven wrote: > On Tue, Jan 23, 2024 at 1:35 AM Kees Cook wrote: >> In an effort to separate intentional arithmetic wrap-around from >> unexpected wrap-around, we need to refactor places that depend on this >> kind of math. One of the most common code patterns of this is: >> >> VAR + value < VAR >> >> Notably, this is considered "undefined behavior" for signed and pointer >> types, which the kernel works around by using the -fno-strict-overflow >> option in the build[1] (which used to just be -fwrapv). Regardless, we >> want to get the kernel source to the position where we can meaningfully >> instrument arithmetic wrap-around conditions and catch them when they >> are unexpected, regardless of whether they are signed[2], unsigned[3], >> or pointer[4] types. >> >> Refactor open-coded unsigned wrap-around addition test to use >> check_add_overflow(), retaining the result for later usage (which removes >> the redundant open-coded addition). This paves the way to enabling the >> unsigned wrap-around sanitizer[2] in the future. >> >> Link: https://git.kernel.org/linus/68df3755e383e6fecf2354a67b08f92f18536594 [1] >> Link: https://github.com/KSPP/linux/issues/26 [2] >> Link: https://github.com/KSPP/linux/issues/27 [3] >> Link: https://github.com/KSPP/linux/issues/344 [4] >> Cc: Geert Uytterhoeven >> Cc: Andrew Morton >> Cc: Arnd Bergmann >> Cc: Liam Howlett >> Cc: "Matthew Wilcox (Oracle)" >> Cc: Hugh Dickins >> Cc: linux-m68k@lists.linux-m68k.org >> Signed-off-by: Kees Cook > > Thanks for your patch! > >> --- a/arch/m68k/kernel/sys_m68k.c >> +++ b/arch/m68k/kernel/sys_m68k.c >> @@ -391,10 +391,11 @@ sys_cacheflush (unsigned long addr, int scope, int cache, unsigned long len) >> >> mmap_read_lock(current->mm); >> } else { >> + unsigned long sum; > > "sum" sounds like this is a dummy variable, to please the third > parameter of check_add_overflow()... > >> struct vm_area_struct *vma; >> >> /* Check for overflow. */ > > I agree with Liam: please drop the comment. > >> - if (addr + len < addr) >> + if (check_add_overflow(addr, len, &sum)) >> goto out; >> >> /* >> @@ -403,7 +404,7 @@ sys_cacheflush (unsigned long addr, int scope, int cache, unsigned long len) >> */ >> mmap_read_lock(current->mm); >> vma = vma_lookup(current->mm, addr); >> - if (!vma || addr + len > vma->vm_end) >> + if (!vma || sum > vma->vm_end) > > ... Oh, it is actually used. What about renaming it to "end" instead? IMHO this is more descriptive: + if (check_add_overflow(addr, len, &sum)) than this: + if (check_add_overflow(addr, len, &end)) "sum" is IMHO quite obviously sum of the preceding args, whereas I do not know what "end" would be. - Eero >> goto out_unlock; >> } > > With the above fixed: > > Reviewed-by: Geert Uytterhoeven > Acked-by: Geert Uytterhoeven > > If you want me to take this through the m68k tree (for v6.9), please > let me know. > Thanks! > > Gr{oetje,eeting}s, > > Geert >