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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F3FF2C433EF for ; Mon, 20 Dec 2021 16:38:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238537AbhLTQiO convert rfc822-to-8bit (ORCPT ); Mon, 20 Dec 2021 11:38:14 -0500 Received: from mail-eopbgr120053.outbound.protection.outlook.com ([40.107.12.53]:17680 "EHLO FRA01-PR2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S238685AbhLTQiM (ORCPT ); Mon, 20 Dec 2021 11:38:12 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HuqtrAq6Wtlc2rD/0LhRbz5/9myZJPOto4euONQeVO1JLbxVKqsOoy4qfJAi88YjNEmSfEFWmO59cx+YyE5nkXvueosAOX9WNLr9YetOn4Yb1ebXDwOzsNkEiKwtdQvNhoE5qFBRUf7nYB+fc2tapOqltjzo+hjHZ9vSVT9AFxqWnRWnjVd/yZhkLnVP1femWg5e9W63pnUS2AtszaSwJrNNSwGhrtRf5/n4OTEZ8GKqQRj5XGwFZSKkHd0/Bqxp/OpBwkoE9m+yZP1KvJJq0rkWBl8WBesqLYlnAarIOeGqPHzd4EYrog4IesOtaHkscF7CX/jn0nFFxOnRIvuT6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=YjkLzccxBgRUBZrcs677amlRn+CXlywVi4YSlZ0Z6Bk=; b=RQSugFjHwPSP434xZTVF5Wn2JgkZ+cozfxrxear8imbq4kx7VxaGKVaBUYv93TvCF/6K23Q2l9DdRs8+Nm+27JEE0Of1lVoAEn0agCwJ1EK6YX+G8CqBlc1J9vyo96WIkwB+qj1QM+c26slfz5AZJQdqPHVOT9qo9FA+hWyxUVU/gTCp0/ADe16WTC+MYNjhortVB58BaPD8RzaDJXv5qlMCjVklFkEdGBMEkCUz8870fZjBV5pP5CmLy3E8Nf7IX+RRqKQZCNaTfz1wAhfgIm82yh3y9q+l+IoOAYHEbCwm/lhsdJmE5+xAnGNu7cK58YMpZuFw4k3CZ/+oPD1RpA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=csgroup.eu; dmarc=pass action=none header.from=csgroup.eu; dkim=pass header.d=csgroup.eu; arc=none Received: from MRZP264MB2988.FRAP264.PROD.OUTLOOK.COM (2603:10a6:501:31::15) by MR1P264MB2161.FRAP264.PROD.OUTLOOK.COM (2603:10a6:501:14::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4801.15; Mon, 20 Dec 2021 16:38:09 +0000 Received: from MRZP264MB2988.FRAP264.PROD.OUTLOOK.COM ([fe80::f0ef:856d:b0de:e85d]) by MRZP264MB2988.FRAP264.PROD.OUTLOOK.COM ([fe80::f0ef:856d:b0de:e85d%5]) with mapi id 15.20.4801.020; Mon, 20 Dec 2021 16:38:09 +0000 From: Christophe Leroy To: Josh Poimboeuf , Jiri Kosina , Miroslav Benes , Petr Mladek , Joe Lawrence , Steven Rostedt , Ingo Molnar , "Naveen N . Rao" CC: Christophe Leroy , "linux-kernel@vger.kernel.org" , "linuxppc-dev@lists.ozlabs.org" , "live-patching@vger.kernel.org" , Russell Currey Subject: [PATCH v2 03/13] powerpc/module_32: Fix livepatching for RO modules Thread-Topic: [PATCH v2 03/13] powerpc/module_32: Fix livepatching for RO modules Thread-Index: AQHX9b/55CfsfkREwkW5lW2tFTV26g== Date: Mon, 20 Dec 2021 16:38:09 +0000 Message-ID: References: In-Reply-To: Accept-Language: fr-FR, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=csgroup.eu; x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 6484999f-1959-4747-8320-08d9c3d71b9b x-ms-traffictypediagnostic: MR1P264MB2161:EE_ x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:2887; x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 440SdqYZHu3Y98exsSrbVVTqc7r1xusAGUTHX5cl3UfQV3A7bJHh6dbvNqeWTIIJeI+l5ViGT0nr8gzKq2ZW3C0SEJa86kBXiyIXSAE3QMoPThY51hl44Myd4ixvbbCE9bmt1C1NCSPsLck3G0vXXKMtQwIkbB+zlF0OXD5WFzoS2wXQ4rN2UW89f17WFwJKrDTZyS8d1TmUvbg3i7feuA0ACwUN5xKLhypOsvS9eenDuQN8TuO6cUO8ddGu6BBM7696DEsHC+MSfhWC9q6DB7KaAsEXuEVLg5ilZtc9sQrBc+25Z6KmeMVehNSeetoYFapM9OJOI2GrOet8ncuGk19C1n0If4+Z0z205Rt9D4fYUE+k4uWGijw9WjjPOuy+8ZwELO1JO0xwme2qKINrm+cRhgM1FFjw9FAh3EPBUQiXUTEeZCNa7yYyxOwq32HHQuh8O7Ef1qNV3QKBuZcxwLM6eQDpbMp2qyKgd6W8AqBAzhgj9FDFEHHCphhSGcqIM/F+x6aZGlHnvFyH9WsgSOBadhxe8a3rnMUEL9aoI7L4xYIKA8gpZSxAgGIpzo8guYTEUsSRDylODAkR9dyWMjr/UxwHj4A3p51ocDhGdAiTqhDWXxLWGRUQdyS0rxGcuO9t+Ydc8vce32CGQmbAhmLlYwFlPuZ99muPFaClm6qX2H54mIBJvbN9+M0EW2WfzfT4BN6G1GUV3Cyf7IO5zw== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:MRZP264MB2988.FRAP264.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(4636009)(366004)(44832011)(26005)(316002)(110136005)(6486002)(186003)(54906003)(122000001)(8936002)(36756003)(64756008)(76116006)(7416002)(66556008)(66476007)(91956017)(38100700002)(2906002)(66946007)(66446008)(38070700005)(83380400001)(5660300002)(508600001)(6506007)(4326008)(6512007)(86362001)(2616005)(8676002)(71200400001);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?Q?sG3rK+HFlesylhsqjaYVrVBujlPgBXZSrkp8JsfKNhEMprIMLMdxQho1Je?= =?iso-8859-1?Q?ofzHS4PmcnkMDLIKEFUgF8TNO1eYHeFwxXqE9rlWvKn4FCBKQZdbP2hP6I?= =?iso-8859-1?Q?sTCMDGCGIYVWn0B4kd02oGkd7fy4miho61ZZdMqQAG1HOkN6iecHPT9f8U?= =?iso-8859-1?Q?f8igSBL6Nba1usoZSIcUap8GWqsJnpnYAYYCFrnileK52oNrqqXKEvBMhm?= =?iso-8859-1?Q?EDFjEbHyOd9hwICWZUfnQCz2cL4HLkxDfsIiWYhKzeYpO9cjd5RSyogz5Q?= =?iso-8859-1?Q?9BKbmAVchV1c8nKRuNh1xipn19F0Scdl4O9NDGLB52LUwIWFMrh5mIC9bE?= =?iso-8859-1?Q?zS3JX/9UqZQyx0NrmsMwuxAgn57H5pEpnl0JMsJNZ7Jz/ow72zstoH+xkw?= =?iso-8859-1?Q?cIvq3Cj7Le86kNzvxwkRN5jI5zEdEKdfg1Kaz9oYhsZ8tfOvAqZMl96Pw0?= =?iso-8859-1?Q?YKbf6HhzOA/YgOwdA0KkNZ/BN05Tb9eCtir5s1sXuP8VJDpvL0yK5xr5TZ?= =?iso-8859-1?Q?BKpxMmv0jsAKMphDLQJX+ek5COa4UtmWQMGuHxbMN3XOV9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TeS7V4/TMY+jiuf6VkS/+wMSChtG+pTzdJn1bNGX09ukEgqSFXgAf?= =?iso-8859-1?Q?St9AmhbJUDzMa+HRKYNAh/+ltuKqKq+zgD8ofBMW+NCmdCM+8T3dnyxYHU?= =?iso-8859-1?Q?5e46BaAFOn323JjU1vl4AKBFbpTr7lghbjrq+OlyhS1OEMTsCsURfB3r9I?= =?iso-8859-1?Q?l6D5sRY1HGdMr8oejXv4OBbwbpvGcrdnXhyELlQ2HYm93uuueGpWiEY4NG?= =?iso-8859-1?Q?JUb7BeT317zj8TpcwDCvEbYTW8qaDgl9vYoGDFMdiRicm2w8YN15QxGo8S?= =?iso-8859-1?Q?Og5zRtc2j/qySduheX+D/AV1w=3D?= Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 8BIT MIME-Version: 1.0 X-OriginatorOrg: csgroup.eu X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: MRZP264MB2988.FRAP264.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-Network-Message-Id: 6484999f-1959-4747-8320-08d9c3d71b9b X-MS-Exchange-CrossTenant-originalarrivaltime: 20 Dec 2021 16:38:09.7416 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 9914def7-b676-4fda-8815-5d49fb3b45c8 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: GZHRbB1aDTXT1HxJdy/bGrJCSDMuaIcXXMixkTFZh2jx5D95+dnW4BhRrNxQqMot8ZDOYglbc0jXjxmyKRcbyxa7XncZHBAsA+KpN6oDxBA= X-MS-Exchange-Transport-CrossTenantHeadersStamped: MR1P264MB2161 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Livepatching a loaded module involves applying relocations through apply_relocate_add(), which attempts to write to read-only memory when CONFIG_STRICT_MODULE_RWX=y. R_PPC_ADDR16_LO, R_PPC_ADDR16_HI, R_PPC_ADDR16_HA and R_PPC_REL24 are the types generated by the kpatch-build userspace tool or klp-convert kernel tree observed applying a relocation to a post-init module. Use patch_instruction() to patch those relocations. Commit 8734b41b3efe ("powerpc/module_64: Fix livepatching for RO modules") did similar change in module_64. Signed-off-by: Christophe Leroy Cc: Russell Currey --- arch/powerpc/kernel/module_32.c | 44 ++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/arch/powerpc/kernel/module_32.c b/arch/powerpc/kernel/module_32.c index a491ad481d85..a0432ef46967 100644 --- a/arch/powerpc/kernel/module_32.c +++ b/arch/powerpc/kernel/module_32.c @@ -18,6 +18,7 @@ #include #include #include +#include /* Count how many different relocations (different symbol, different addend) */ @@ -174,15 +175,25 @@ static uint32_t do_plt_call(void *location, entry++; } - entry->jump[0] = PPC_RAW_LIS(_R12, PPC_HA(val)); - entry->jump[1] = PPC_RAW_ADDI(_R12, _R12, PPC_LO(val)); - entry->jump[2] = PPC_RAW_MTCTR(_R12); - entry->jump[3] = PPC_RAW_BCTR(); + if (patch_instruction(&entry->jump[0], ppc_inst(PPC_RAW_LIS(_R12, PPC_HA(val))))) + return 0; + if (patch_instruction(&entry->jump[1], ppc_inst(PPC_RAW_ADDI(_R12, _R12, PPC_LO(val))))) + return 0; + if (patch_instruction(&entry->jump[2], ppc_inst(PPC_RAW_MTCTR(_R12)))) + return 0; + if (patch_instruction(&entry->jump[3], ppc_inst(PPC_RAW_BCTR()))) + return 0; pr_debug("Initialized plt for 0x%x at %p\n", val, entry); return (uint32_t)entry; } +static int patch_location_16(uint32_t *loc, u16 value) +{ + loc = PTR_ALIGN_DOWN(loc, sizeof(u32)); + return patch_instruction(loc, ppc_inst((*loc & 0xffff0000) | value)); +} + int apply_relocate_add(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex, @@ -216,37 +227,42 @@ int apply_relocate_add(Elf32_Shdr *sechdrs, case R_PPC_ADDR16_LO: /* Low half of the symbol */ - *(uint16_t *)location = value; + if (patch_location_16(location, PPC_LO(value))) + return -EFAULT; break; case R_PPC_ADDR16_HI: /* Higher half of the symbol */ - *(uint16_t *)location = (value >> 16); + if (patch_location_16(location, PPC_HI(value))) + return -EFAULT; break; case R_PPC_ADDR16_HA: - /* Sign-adjusted lower 16 bits: PPC ELF ABI says: - (((x >> 16) + ((x & 0x8000) ? 1 : 0))) & 0xFFFF. - This is the same, only sane. - */ - *(uint16_t *)location = (value + 0x8000) >> 16; + if (patch_location_16(location, PPC_HA(value))) + return -EFAULT; break; case R_PPC_REL24: if ((int)(value - (uint32_t)location) < -0x02000000 - || (int)(value - (uint32_t)location) >= 0x02000000) + || (int)(value - (uint32_t)location) >= 0x02000000) { value = do_plt_call(location, value, sechdrs, module); + if (!value) + return -EFAULT; + } /* Only replace bits 2 through 26 */ pr_debug("REL24 value = %08X. location = %08X\n", value, (uint32_t)location); pr_debug("Location before: %08X.\n", *(uint32_t *)location); - *(uint32_t *)location - = (*(uint32_t *)location & ~0x03fffffc) + value = (*(uint32_t *)location & ~0x03fffffc) | ((value - (uint32_t)location) & 0x03fffffc); + + if (patch_instruction(location, ppc_inst(value))) + return -EFAULT; + pr_debug("Location after: %08X.\n", *(uint32_t *)location); pr_debug("ie. jump to %08X+%08X = %08X\n", -- 2.33.1