From patchwork Mon Mar 19 16:12:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 132061 Delivered-To: patch@linaro.org Received: by 10.80.152.129 with SMTP id j1csp3019691edb; Mon, 19 Mar 2018 09:37:02 -0700 (PDT) X-Google-Smtp-Source: AG47ELvMAIzy4DvCw6STF8SqmJnVsriHlbVrNPYYXwWKue4NDmLSPjTx74ajs7ZnwzRpt/xbSd7Q X-Received: by 2002:a17:902:a705:: with SMTP id w5-v6mr13401943plq.299.1521477422212; Mon, 19 Mar 2018 09:37:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521477422; cv=none; d=google.com; s=arc-20160816; b=kXO0H/SvdBC5XcgzlprWV8SY2dhwRUbLQ/hXu7BFMyYDDTOUQP1QzqkOmxLXjoEnKy lyN1pNH2XePoYzBbo8UCD9bCsSTJ0RqRAU01d6TQH0HQn/J5iUyNxVS04dA+oAb3Z4u/ MfwK8gb3E0aRwjHyUkqxtbe+MPCvaJmvoR0PJ/B1sI0IuS6nvZQDt0Mu0yxE+KnojXHT SgUyx2+jA/cBkAHCvSGvxpXbTNvx7ciC8RuFwpei/fOR+hOO1jNpvOoAMzdPBJHWSAgi db5I0KtIRdyEkR3+T3P309J4mSc1gfEOsBloZIwOlh8BVBMQ9+T91k3m6/FZ/PJ9e9x7 NQRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :spamdiagnosticmetadata:spamdiagnosticoutput:content-language :accept-language:in-reply-to:references:message-id:date:thread-index :thread-topic:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=fBWJzaQrjBjpqbqG1WvBib7k1e7BM9SutxxxHR8+Gpc=; b=jH+EAZNBEplnB926ODjSFYQBh6gmyekdAKvkDAuUgWnsw6/l/0QBeWlfWCxQ3XqEEj 7rpZkKxYm/GJza6knn69pfPIov2gbyJlQdt2yj5xNb4lLCxagrzKtOdWQWTpFWwQcc2b b6/JN79s/72F3v4VkSULrk53K0UVQeAkC7G/9Vbe1PEh5OvObWYn/jFW+72x5T2J/9C6 tQdN97e1Ai3+/lRzwYPslLnbks1T9oDq+S+Nsa2elTjfOqduBmms/WMOpEEbWwhkYfaY jg7xR4z17e4J2mJARpTCUVBPY4ram2moSV8h9fcBR0n5lHUImvzvT+RIP4N9wiaui7az CWDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=ZWN1sAPf; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c16si191068pgv.729.2018.03.19.09.37.01; Mon, 19 Mar 2018 09:37:02 -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=pass header.i=@microsoft.com header.s=selector1 header.b=ZWN1sAPf; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S966862AbeCSQg7 (ORCPT + 28 others); Mon, 19 Mar 2018 12:36:59 -0400 Received: from mail-dm3nam03on0102.outbound.protection.outlook.com ([104.47.41.102]:10304 "EHLO NAM03-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S935274AbeCSQNT (ORCPT ); Mon, 19 Mar 2018 12:13:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=fBWJzaQrjBjpqbqG1WvBib7k1e7BM9SutxxxHR8+Gpc=; b=ZWN1sAPfdpBg3xvHZiAK5mSH76lpCAXbPGQaAZLhGpacVQuOW08fmjo1EaCPPoaPmkJv71GSWsnuMHfnd+V8vUUnoLz3v3o4TEeXIJNj9DZvDWXbjA1bgibVGmIJwT+mH72TuOdDDo77SZTFFFxnPI5l8rLC5NOwlNb4yjNvLos= Received: from DM5PR2101MB1032.namprd21.prod.outlook.com (52.132.128.13) by DM5PR2101MB1032.namprd21.prod.outlook.com (52.132.128.13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.631.0; Mon, 19 Mar 2018 16:13:15 +0000 Received: from DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::3d9b:79e7:94eb:5d62]) by DM5PR2101MB1032.namprd21.prod.outlook.com ([fe80::3d9b:79e7:94eb:5d62%5]) with mapi id 15.20.0631.004; Mon, 19 Mar 2018 16:13:15 +0000 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Will Deacon , Sasha Levin Subject: [PATCH AUTOSEL for 3.18 058/102] arm64: futex: Fix undefined behaviour with FUTEX_OP_OPARG_SHIFT usage Thread-Topic: [PATCH AUTOSEL for 3.18 058/102] arm64: futex: Fix undefined behaviour with FUTEX_OP_OPARG_SHIFT usage Thread-Index: AQHTv50byQW6BoIPYECI9wh38nJq2w== Date: Mon, 19 Mar 2018 16:12:41 +0000 Message-ID: <20180319161117.17833-58-alexander.levin@microsoft.com> References: <20180319161117.17833-1-alexander.levin@microsoft.com> In-Reply-To: <20180319161117.17833-1-alexander.levin@microsoft.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [52.168.54.252] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; DM5PR2101MB1032; 7:hPm0UXfFBgaVq2yyH/HDeD5uFK96eTowbJds5uQAt8to3vZVbqn0ypjmcDmu4c1QNJebK4S1uw4o0/9vYr1v7u09wiS458VB+vFJ61EgvRYstRgdq+6qX8r0PJ56MmBcH5jZR5HM2BZfSu5ToD5rgQhm+J3hq7JZ35SLFlm5/13AEHr2z+2B85fdI4MTIqOeoicT9RQtNFp/kAh2EFfKb+j/dGVEMUrhZWdqlAcss7NRoVtif3jC5gRXVOw2Kqrk; 20:8RafqhQizFGqeyzWtwAlCrPKen/Z9BFdMYoeE2wYSMDTsBYHeFff1nTi9Vmmg2lTH12Uo8W6/Y5M7N0eWj/qrgvS/OSIN2Ptwf0Sb0kweWia2MFOyu+kE0pRqKIXdFxSXZoZVPlL8Sh+MOryoiRYAZABKpWykzuSxBls422F260= x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: e4ab8d98-be24-4ef9-8d11-08d58db45254 x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(4652020)(48565401081)(5600026)(4604075)(3008032)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603328)(7193020); SRVR:DM5PR2101MB1032; x-ms-traffictypediagnostic: DM5PR2101MB1032: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(180628864354917)(89211679590171); x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(8211001083)(61425038)(6040522)(2401047)(8121501046)(5005006)(93006095)(93001095)(10201501046)(3231221)(944501300)(52105095)(3002001)(6055026)(61426038)(61427038)(6041310)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123564045)(20161123562045)(20161123558120)(20161123560045)(6072148)(201708071742011); SRVR:DM5PR2101MB1032; BCL:0; PCL:0; RULEID:; SRVR:DM5PR2101MB1032; x-forefront-prvs: 06167FAD59 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(39380400002)(39860400002)(346002)(376002)(366004)(396003)(199004)(189003)(3660700001)(5250100002)(3280700002)(8936002)(3846002)(6116002)(1076002)(10090500001)(105586002)(72206003)(81156014)(8676002)(81166006)(2906002)(5660300001)(97736004)(2501003)(36756003)(6666003)(2900100001)(14454004)(6512007)(6436002)(2950100002)(53936002)(4326008)(54906003)(305945005)(6486002)(25786009)(68736007)(22452003)(316002)(59450400001)(6506007)(86612001)(66066001)(76176011)(102836004)(7736002)(10290500003)(110136005)(106356001)(99286004)(107886003)(26005)(186003)(575784001)(86362001)(478600001)(22906009)(217873001); DIR:OUT; SFP:1102; SCL:1; SRVR:DM5PR2101MB1032; H:DM5PR2101MB1032.namprd21.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: Yk69zFw1oH7DGZRiK9lz5ABOm58w59cPCDfi1Ez7KT3SaFRgtIB6UsgvYOsdoFR/SDLkMlLDczfUQhUvPyaXCDFtipjoK481DMWnHqEyfssLwhK5Hf/N27EBhEuM6XsD4evb2sH01JW3OZJdNCqLZoOR6tVqtIuLuuTFTACs3+RnZoTh/D/8S1RE2Bwdev1Ff1jJnBaVn6BdO6pVgG6lSTsNOmTGuA6tSkWPYBykuVSCHK7JlWCqeGTsmv5OWIOQ1kZWU25n77AWxJTBksYLq6UWYQJiEE2hbvWiavC8Flq7hdU+FIFj3t8BAlx7W/t5AM5fLJvoN5G1syGhque/CA== spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: e4ab8d98-be24-4ef9-8d11-08d58db45254 X-MS-Exchange-CrossTenant-originalarrivaltime: 19 Mar 2018 16:12:41.7486 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR2101MB1032 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Will Deacon [ Upstream commit 5f16a046f8e144c294ef98cd29d9458b5f8273e5 ] FUTEX_OP_OPARG_SHIFT instructs the futex code to treat the 12-bit oparg field as a shift value, potentially leading to a left shift value that is negative or with an absolute value that is significantly larger then the size of the type. UBSAN chokes with: -- 2.14.1 ================================================================================ UBSAN: Undefined behaviour in ./arch/arm64/include/asm/futex.h:60:13 shift exponent -1 is negative CPU: 1 PID: 1449 Comm: syz-executor0 Not tainted 4.11.0-rc4-00005-g977eb52-dirty #11 Hardware name: linux,dummy-virt (DT) Call trace: [] dump_backtrace+0x0/0x538 arch/arm64/kernel/traps.c:73 [] show_stack+0x20/0x30 arch/arm64/kernel/traps.c:228 [] __dump_stack lib/dump_stack.c:16 [inline] [] dump_stack+0x120/0x188 lib/dump_stack.c:52 [] ubsan_epilogue+0x18/0x98 lib/ubsan.c:164 [] __ubsan_handle_shift_out_of_bounds+0x250/0x294 lib/ubsan.c:421 [] futex_atomic_op_inuser arch/arm64/include/asm/futex.h:60 [inline] [] futex_wake_op kernel/futex.c:1489 [inline] [] do_futex+0x137c/0x1740 kernel/futex.c:3231 [] SYSC_futex kernel/futex.c:3281 [inline] [] SyS_futex+0x114/0x268 kernel/futex.c:3249 [] el0_svc_naked+0x24/0x28 ================================================================================ syz-executor1 uses obsolete (PF_INET,SOCK_PACKET) sock: process `syz-executor0' is using obsolete setsockopt SO_BSDCOMPAT This patch attempts to fix some of this by: * Making encoded_op an unsigned type, so we can shift it left even if the top bit is set. * Casting to signed prior to shifting right when extracting oparg and cmparg * Consider only the bottom 5 bits of oparg when using it as a left-shift value. Whilst I think this catches all of the issues, I'd much prefer to remove this stuff, as I think it's unused and the bugs are copy-pasted between a bunch of architectures. Reviewed-by: Robin Murphy Signed-off-by: Will Deacon Signed-off-by: Sasha Levin --- arch/arm64/include/asm/futex.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/arm64/include/asm/futex.h b/arch/arm64/include/asm/futex.h index 5f750dc96e0f..49d057eb93d6 100644 --- a/arch/arm64/include/asm/futex.h +++ b/arch/arm64/include/asm/futex.h @@ -44,16 +44,16 @@ : "memory") static inline int -futex_atomic_op_inuser (int encoded_op, u32 __user *uaddr) +futex_atomic_op_inuser(unsigned int encoded_op, u32 __user *uaddr) { int op = (encoded_op >> 28) & 7; int cmp = (encoded_op >> 24) & 15; - int oparg = (encoded_op << 8) >> 20; - int cmparg = (encoded_op << 20) >> 20; + int oparg = (int)(encoded_op << 8) >> 20; + int cmparg = (int)(encoded_op << 20) >> 20; int oldval = 0, ret, tmp; if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28)) - oparg = 1 << oparg; + oparg = 1U << (oparg & 0x1f); if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32))) return -EFAULT;