From patchwork Mon Oct 24 21:21:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 13018173 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0C84FC67871 for ; Mon, 24 Oct 2022 21:22:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6C1D1940007; Mon, 24 Oct 2022 17:22:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6727494000B; Mon, 24 Oct 2022 17:22:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 56AF0940007; Mon, 24 Oct 2022 17:22:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 4891E940007 for ; Mon, 24 Oct 2022 17:22:09 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 0EE08A0D29 for ; Mon, 24 Oct 2022 21:22:09 +0000 (UTC) X-FDA: 80057115978.21.2AC10E6 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf15.hostedemail.com (Postfix) with ESMTP id 98031A000A for ; Mon, 24 Oct 2022 21:22:08 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id c9-20020a05640227c900b0045d4a88c750so10469831ede.12 for ; Mon, 24 Oct 2022 14:22:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=volBetDwZhQX9fC04zMVYOMbO1RkS1wEneNzDmowndU=; b=J4G17c7UDbBB6vaIpcTMG+Yagy71XxidcSxJ/NpEIZ/jbOzoWk0kfwY4J5fIF7ruNg uGi67M7F6hDRJg9OeFqAKsiOneRo4mQZvIZQBucP08UFRGzIzuGOszxXz8ZopyHKDuIS IULIJgo8TuuvhCkpN2E1BwhlE/tvb0ROX1J8+selQyU1FbEVIVZjgsRAfyWC7ZcYSyo4 UjBJSA65gpAUSXRfDlsMoLWLxjJd5ueHDjT7imXUcZo67G6nmMBYnLAJ2fFWILv1HmXJ loSrTZVNG/l/jpFZB3PUMQp3G0hSpSh8ejfuFn4zQ+5FICyuMUvWK71XzUK5hasUFpYa eOGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=volBetDwZhQX9fC04zMVYOMbO1RkS1wEneNzDmowndU=; b=DwkIiCO1A1m/cXs7zbxoLv7o04SUjhRhUgDGqGlekEWGKfK2pwL9CyVdtaQAjh8YMD gqGpcOQXp49e06MIPGedYuW6ekeAE28h8O7NOFOi49g+lNXj3pREZKO4sGGYnCyVHMmm DghHGgaN/3vsHpqpCFyxBSvfM8sE89wAzhN5NMLXOFuEbJFinI20AuptoChJQJIbnN9h M/2EIiOhDdkyISfPTjeRDWG1Q2R4wdcR69bY/P/oedCSnibStcaZXaa9MxpdqEkSNZwf XXQjeW5Q9aCoQ4DeaRInx4WE0Rul3/tvBLK0kwutm7VuiEZ46Dnh0hyf/iMCEOkBwM7P St+w== X-Gm-Message-State: ACrzQf371j2bI7g2xgj7Djd8dbz3XWduKBsd++cuNWaJ47MaSFTFm0uQ sXq3vYum0j2vdDU/MRdQlpoLRypt9Ug= X-Google-Smtp-Source: AMsMyM6Tq20dngPWWsHMTzhzn39uWvW18mFyDgT+su4EaQ1IkASlw4U74YHbIUx7kj1tUnUMLAaBPw2aqg0= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:8695:7c88:66ed:f9d1]) (user=glider job=sendgmr) by 2002:a17:907:3f96:b0:7ab:34aa:9094 with SMTP id hr22-20020a1709073f9600b007ab34aa9094mr2182051ejc.85.1666646527998; Mon, 24 Oct 2022 14:22:07 -0700 (PDT) Date: Mon, 24 Oct 2022 23:21:43 +0200 In-Reply-To: <20221024212144.2852069-1-glider@google.com> Mime-Version: 1.0 References: <20221024212144.2852069-1-glider@google.com> X-Mailer: git-send-email 2.38.0.135.g90850a2211-goog Message-ID: <20221024212144.2852069-4-glider@google.com> Subject: [PATCH 4/5] x86: asm: make sure __put_user_size() evaluates pointer once From: Alexander Potapenko To: glider@google.com Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Andrew Morton , Borislav Petkov , Ingo Molnar , Thomas Gleixner , x86@kernel.org, youling257 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666646528; a=rsa-sha256; cv=none; b=8of3Wi1DIXTTQSMMFb9JtU1Pv7pLzdozBPjvvAL4xd9bLbbj3ux3njKnKKVRdwXZDhhkEC jy5BD9OIC/bL0ozusAkV4vM78ThX0ZhkYFSv+4Rc0fGaapqgLVuuG2L3ZPIrNigok7AxTz KqD99PZorkbk3oaGW6vNNagHREo/Dek= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=J4G17c7U; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf15.hostedemail.com: domain of 3_wFXYwYKCIAkpmhivksskpi.gsqpmry1-qqozego.svk@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3_wFXYwYKCIAkpmhivksskpi.gsqpmry1-qqozego.svk@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666646528; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=volBetDwZhQX9fC04zMVYOMbO1RkS1wEneNzDmowndU=; b=URnlSkpqGV9S184S/dnYAaw9JSivvOaANVQsQi8yh+zFfULDEZ4OG5B6st2v2rhXGO31iB 2gaKb6oECzsK+fAPrc4MMR/GSRxeIKy1piWy+0qb7Cz7+m/oVVJWhuDpe42lFb8hGhYuXT fd30uxIYvb1HsSyblp7hY08lCykrspw= X-Rspamd-Queue-Id: 98031A000A Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=J4G17c7U; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf15.hostedemail.com: domain of 3_wFXYwYKCIAkpmhivksskpi.gsqpmry1-qqozego.svk@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3_wFXYwYKCIAkpmhivksskpi.gsqpmry1-qqozego.svk@flex--glider.bounces.google.com X-Rspamd-Server: rspam12 X-Rspam-User: X-Stat-Signature: h97s4rgq68667rstximsybwhnswe7q45 X-HE-Tag: 1666646528-251909 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: User access macros must ensure their arguments are evaluated only once if they are used more than once in the macro body. Adding instrument_put_user() to __put_user_size() resulted in double evaluation of the `ptr` argument, which led to correctness issues when performing e.g. unsafe_put_user(..., p++, ...). To fix those issues, evaluate the `ptr` argument of __put_user_size() at the beginning of the macro. Cc: Andrew Morton Cc: Borislav Petkov Cc: Ingo Molnar Cc: Thomas Gleixner Cc: x86@kernel.org Reported-by: youling257 Signed-off-by: Alexander Potapenko Fixes: 888f84a6da4d ("x86: asm: instrument usercopy in get_user() and put_user()") --- arch/x86/include/asm/uaccess.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h index 8bc614cfe21b9..1cc756eafa447 100644 --- a/arch/x86/include/asm/uaccess.h +++ b/arch/x86/include/asm/uaccess.h @@ -254,24 +254,25 @@ extern void __put_user_nocheck_8(void); #define __put_user_size(x, ptr, size, label) \ do { \ __typeof__(*(ptr)) __x = (x); /* eval x once */ \ - __chk_user_ptr(ptr); \ + __typeof__(ptr) __ptr = (ptr); /* eval ptr once */ \ + __chk_user_ptr(__ptr); \ switch (size) { \ case 1: \ - __put_user_goto(__x, ptr, "b", "iq", label); \ + __put_user_goto(__x, __ptr, "b", "iq", label); \ break; \ case 2: \ - __put_user_goto(__x, ptr, "w", "ir", label); \ + __put_user_goto(__x, __ptr, "w", "ir", label); \ break; \ case 4: \ - __put_user_goto(__x, ptr, "l", "ir", label); \ + __put_user_goto(__x, __ptr, "l", "ir", label); \ break; \ case 8: \ - __put_user_goto_u64(__x, ptr, label); \ + __put_user_goto_u64(__x, __ptr, label); \ break; \ default: \ __put_user_bad(); \ } \ - instrument_put_user(__x, ptr, size); \ + instrument_put_user(__x, __ptr, size); \ } while (0) #ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT