Received: by 10.223.185.116 with SMTP id b49csp1103902wrg; Sat, 3 Mar 2018 15:40:22 -0800 (PST) X-Google-Smtp-Source: AG47ELs6SJsOG2+7f11UgGOnsLSni+Gb8W2t+XdsnnviE3DfvLwBv8+wf0WvtRvJw8YeSn07B7GI X-Received: by 2002:a17:902:424:: with SMTP id 33-v6mr9087740ple.433.1520120422299; Sat, 03 Mar 2018 15:40:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520120422; cv=none; d=google.com; s=arc-20160816; b=RkKkhGhBUAam+3F1wHXXeqlMvOVUEh01ezFFDbGdq9jLJmEOoQ+jhgN5Pi1EqQ8yI8 o8DjGKyJLbHawtfXus9b6XL95ocHAq41vDBsXi+Gh58L+gYFV2z2qq1SvmpBbNsY2Vi9 V2hT16IlKV/F3SV2teG++ptJQlbO7KkaIF66TFWtBqVPmOWK0P3bRzk73uUlWWvbJYhA 36Y48dg2SLJx6bx4x4XddJJNj+JoNz8FfjPFEQwzOvSP+eHs4X+b6arts9xNNvBQdCGC UGBSVPPSuWT4zt5UGswcTBhCsk03u5nt7ooJgJlICa8e902fhqDgV/NYoEajH6g1JPHZ 9GQQ== 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 :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=lZvIOpxAKAUQjfTEqhcTsWAfUoi/BGMG0+3P8aO2G5U=; b=FIylbdEtreJEMf58eBfUsNHyLNq0saTcKW2hGlNFoYtPqkT3Km1fAYGbSyPGypve6i LV3AMHKadY+qAc5x1thuaINUOG0DBNtblMP9UKzVutdB61C8OWSugZt9C/dTAySH0LlR l7qm5DlJ2Vydq7Z/Wyr01YKUUUV5Kt060SG98Q1oZHHS352eZX7dfIs16HGe45HVp5sh 9S+Yx7buZevWy2NWN4/MPRLKiilcQ0AtWR/Wf+Z7Xd5oHXCUlW2tw95kUDgQWVqW9Doh qTptcVIZrfwDbBaAC2ygvwcRKBNH7+uU+tW91B7xyTD7eoHynEYrYvWq9HynzugOpw3w C0sQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector1 header.b=R1IIZNWA; 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 k64si4006984pgk.256.2018.03.03.15.40.08; Sat, 03 Mar 2018 15:40:22 -0800 (PST) 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=R1IIZNWA; 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 S934344AbeCCWfL (ORCPT + 99 others); Sat, 3 Mar 2018 17:35:11 -0500 Received: from mail-sn1nam01on0132.outbound.protection.outlook.com ([104.47.32.132]:20800 "EHLO NAM01-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933677AbeCCWfG (ORCPT ); Sat, 3 Mar 2018 17:35:06 -0500 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=lZvIOpxAKAUQjfTEqhcTsWAfUoi/BGMG0+3P8aO2G5U=; b=R1IIZNWAMSf5rYIoJ/52jjtRd0+E8W2HhBPq0CRj539uHaMh/ZUZJrPSjdT7NdZq7YTMC1QtZBZXTm+P/fh3+WPWqmg1IpQNp9yd/y4F9G4EyZtXpxgpUlOH0ZipnA9xXnzP5a9jwIfnCDrpp4609iimfvynYh/nt6tADf/sE6w= Received: from MW2PR2101MB1034.namprd21.prod.outlook.com (52.132.149.10) by MW2PR2101MB0924.namprd21.prod.outlook.com (52.132.152.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.588.3; Sat, 3 Mar 2018 22:34:53 +0000 Received: from MW2PR2101MB1034.namprd21.prod.outlook.com ([fe80::1d56:338f:e2b:cec0]) by MW2PR2101MB1034.namprd21.prod.outlook.com ([fe80::1d56:338f:e2b:cec0%3]) with mapi id 15.20.0567.006; Sat, 3 Mar 2018 22:34:53 +0000 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: David Daney , James Hogan , Alexei Starovoitov , "Steven J . Hill" , "linux-mips@linux-mips.org" , "netdev@vger.kernel.org" , Ralf Baechle , Sasha Levin Subject: [PATCH AUTOSEL for 4.9 136/219] MIPS: BPF: Quit clobbering callee saved registers in JIT code. Thread-Topic: [PATCH AUTOSEL for 4.9 136/219] MIPS: BPF: Quit clobbering callee saved registers in JIT code. Thread-Index: AQHTsz8PK5JntFS4qUW775AL7VNf+g== Date: Sat, 3 Mar 2018 22:29:15 +0000 Message-ID: <20180303222716.26640-136-alexander.levin@microsoft.com> References: <20180303222716.26640-1-alexander.levin@microsoft.com> In-Reply-To: <20180303222716.26640-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;MW2PR2101MB0924;6:U16MWaDrpJX6Z2hfjObAF4sLAv5wZ9DisZPa/CSY5sK0iCaJ8gOYY9DLvOEooGd7nNgCMAMfttVR2huLteVs1t0r/D0u1Wjz7U8X8eD5iFU4eAP8nXBcx+Y7xDWPDlQ8OLAZhToVeAfDFXO5mgGV0l/mRiqL5YqdjD5GNS8Q8x6jvFBaNMEoXoBQWhe1fp17n7fhctIMjc0dFpDrisnPgpAiVuNiB3MiVnvhMYPOr4OKWU9PpWjHj4B+w45g5fqOOQKjbPRyB3sWANk8oTNyQRcyabclsYngazcvDHxihEGAazXoEkmDk3xY3ODbCSb3pAln6uW2hC/3q4eI3pj6vidZsaIG8A5RHodzVd4nJPQ=;5:a3uSITIBJg2kyE/ExC83qr8h+WBHRjmSftEZAno/+7sFNrEFh5sfdaNVy2aBQydaWOU+3jh9sDr83cwWLkHD3wo2Jdmum3Uw9Oycw0errj85kyxVxVDm9K/sx3suqC8co7WxDJJebnOM6pgL19EOsEExGvVgtCYCaccgtheLOjM=;24:fqpNH8MFcoiupjdvNTtkxRsXnfVhlY2o+nnuO8Sus3Wbbk8e2LHp0EFd4O2ud8dZxkYsLla/KVktxOC4a5sw0Tid8ZYLnnHX2NjactqZsx4=;7:190pspeuHsVITWH598IT+yiq7i6LYH+LnjiHOw2LXxIBiF+tx9Xh6kuJD5RQpfok69GSiEiEtqjhmMp91RtqDDI9CkJrnhcmnknSA5XxLqzxT5DHzk71lOYvqvLe8UCbMFxU2sTr8zAWPviVhft74+Ujzg7x9vE9BXciFHO8/FiqP4zOOw1SePwyrarj7qbuWPEHiaz/I/k+BirAvRufeDPH/vtla7zvHM/QJ+XKaSfYL/+ANUEiJPGdmZ7XCHCA x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: 7e9c1ab5-4a81-4d7b-69b7-08d58156fc09 x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(48565401081)(5600026)(4604075)(3008032)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603307)(7193020);SRVR:MW2PR2101MB0924; x-ms-traffictypediagnostic: MW2PR2101MB0924: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Alexander.Levin@microsoft.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(28532068793085)(89211679590171)(9452136761055); x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(8211001083)(61425038)(6040501)(2401047)(8121501046)(5005006)(93006095)(93001095)(10201501046)(3231220)(944501244)(52105095)(3002001)(6055026)(61426038)(61427038)(6041288)(20161123562045)(20161123558120)(20161123564045)(20161123560045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(6072148)(201708071742011);SRVR:MW2PR2101MB0924;BCL:0;PCL:0;RULEID:;SRVR:MW2PR2101MB0924; x-forefront-prvs: 0600F93FE1 x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(346002)(376002)(39860400002)(396003)(366004)(39380400002)(189003)(199004)(102836004)(2950100002)(10090500001)(6666003)(316002)(26005)(186003)(25786009)(2900100001)(2906002)(54906003)(22452003)(110136005)(97736004)(6506007)(59450400001)(7736002)(4326008)(305945005)(5250100002)(86362001)(575784001)(2501003)(99286004)(68736007)(72206003)(53936002)(6436002)(6116002)(3846002)(5660300001)(66066001)(1076002)(76176011)(14454004)(966005)(36756003)(3660700001)(6486002)(86612001)(81156014)(8676002)(81166006)(10290500003)(6512007)(8936002)(3280700002)(478600001)(107886003)(106356001)(6306002)(105586002)(22906009)(217873001);DIR:OUT;SFP:1102;SCL:1;SRVR:MW2PR2101MB0924;H:MW2PR2101MB1034.namprd21.prod.outlook.com;FPR:;SPF:None;PTR:InfoNoRecords;MX:1;A:1;LANG:en; received-spf: None (protection.outlook.com: microsoft.com does not designate permitted sender hosts) Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7e9c1ab5-4a81-4d7b-69b7-08d58156fc09 X-MS-Exchange-CrossTenant-originalarrivaltime: 03 Mar 2018 22:29:15.3382 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW2PR2101MB0924 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: David Daney [ Upstream commit 1ef0910cfd681f0bd0b81f8809935b2006e9cfb9 ] If bpf_needs_clear_a() returns true, only actually clear it if it is ever used. If it is not used, we don't save and restore it, so the clearing has the nasty side effect of clobbering caller state. Also, don't emit stack pointer adjustment instructions if the adjustment amount is zero. Signed-off-by: David Daney Cc: James Hogan Cc: Alexei Starovoitov Cc: Steven J. Hill Cc: linux-mips@linux-mips.org Cc: netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org Patchwork: https://patchwork.linux-mips.org/patch/15745/ Signed-off-by: Ralf Baechle Signed-off-by: Sasha Levin --- arch/mips/net/bpf_jit.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/arch/mips/net/bpf_jit.c b/arch/mips/net/bpf_jit.c index 49a2e2226fee..248603739198 100644 --- a/arch/mips/net/bpf_jit.c +++ b/arch/mips/net/bpf_jit.c @@ -526,7 +526,8 @@ static void save_bpf_jit_regs(struct jit_ctx *ctx, unsi= gned offset) u32 sflags, tmp_flags; =20 /* Adjust the stack pointer */ - emit_stack_offset(-align_sp(offset), ctx); + if (offset) + emit_stack_offset(-align_sp(offset), ctx); =20 tmp_flags =3D sflags =3D ctx->flags >> SEEN_SREG_SFT; /* sflags is essentially a bitmap */ @@ -578,7 +579,8 @@ static void restore_bpf_jit_regs(struct jit_ctx *ctx, emit_load_stack_reg(r_ra, r_sp, real_off, ctx); =20 /* Restore the sp and discard the scrach memory */ - emit_stack_offset(align_sp(offset), ctx); + if (offset) + emit_stack_offset(align_sp(offset), ctx); } =20 static unsigned int get_stack_depth(struct jit_ctx *ctx) @@ -625,8 +627,14 @@ static void build_prologue(struct jit_ctx *ctx) if (ctx->flags & SEEN_X) emit_jit_reg_move(r_X, r_zero, ctx); =20 - /* Do not leak kernel data to userspace */ - if (bpf_needs_clear_a(&ctx->skf->insns[0])) + /* + * Do not leak kernel data to userspace, we only need to clear + * r_A if it is ever used. In fact if it is never used, we + * will not save/restore it, so clearing it in this case would + * corrupt the state of the caller. + */ + if (bpf_needs_clear_a(&ctx->skf->insns[0]) && + (ctx->flags & SEEN_A)) emit_jit_reg_move(r_A, r_zero, ctx); } =20 --=20 2.14.1