From nobody Sun Dec 22 01:04:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1727253808; cv=none; d=zohomail.com; s=zohoarc; b=eK0Sq3rMJYEqKzX8JSX8Xg9oD36c8fckmjdE60W2GvhIzhNIBf3MRE/xXaq6YiM2s11EgwEtZGmut3OS0qW0olkGxAONhMMa7/tzX/ptmsYdNn1kldSjHOtI2TQZjUZd5SIlMnc1JA5VY5S2gn9lkYE62y/h48xGRSe5Fd+uBI0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1727253808; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=/A7anrJKRONUakwDhBF6Xlt3dpj6kBkDkGt4BhpDYoU=; b=OSabw0d6NGskLnbQACh1w+NBR22A/qUrMLsD+EjGUxXRxuh7ze4dodsC1LZ9zjB8YvjA+65mPJYTnWrY8Xh5uqdu7niRxDJgiaMi1aav9PI7qV2M1CdPOoiqMr+I1A53Y+r6B8TuEaYxcFm6XXXcpIiDqIFo7BjftOJL67bif8A= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 172725380802955.32454498902632; Wed, 25 Sep 2024 01:43:28 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.803575.1214260 (Exim 4.92) (envelope-from ) id 1stNc6-000661-60; Wed, 25 Sep 2024 08:43:10 +0000 Received: by outflank-mailman (output) from mailman id 803575.1214260; Wed, 25 Sep 2024 08:43:10 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1stNc6-00065S-16; Wed, 25 Sep 2024 08:43:10 +0000 Received: by outflank-mailman (input) for mailman id 803575; Wed, 25 Sep 2024 08:43:08 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1stNc4-0005cc-ID for xen-devel@lists.xenproject.org; Wed, 25 Sep 2024 08:43:08 +0000 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [2a00:1450:4864:20::636]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 2fda582a-7b1a-11ef-a0ba-8be0dac302b0; Wed, 25 Sep 2024 10:43:07 +0200 (CEST) Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-a910860e4dcso445900166b.3 for ; Wed, 25 Sep 2024 01:43:07 -0700 (PDT) Received: from localhost ([213.195.124.163]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a93a5d4a6c8sm40342966b.222.2024.09.25.01.43.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Sep 2024 01:43:06 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 2fda582a-7b1a-11ef-a0ba-8be0dac302b0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.com; s=google; t=1727253787; x=1727858587; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/A7anrJKRONUakwDhBF6Xlt3dpj6kBkDkGt4BhpDYoU=; b=dlGUN5i9SeeJh08PT5HlEaXjzObD/cux0syuttMW7JUtfLQsLIMLRqTunqb8lHQopa cOGGhCWZVd/IrzqQb43a9YSEzSsE3sHTfm+YgmLMXmsQtJh+cv21Pom5POAT0BVpkodK EDR6ChtmGXx1ijrqmSQiOjR83guc2TqIT1Zr0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727253787; x=1727858587; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/A7anrJKRONUakwDhBF6Xlt3dpj6kBkDkGt4BhpDYoU=; b=BFR0P9nWomMH/vT6InMxWysltFefRjY16eOVhK7jXHvYhp8fEh7v8FCPO2bbUqQpfh FHJwgu5z+qjxXnpAamYO+WNGhpEbjgF8bnuLVeUxgEdapYbBa/W3cm60eoPx4yrCy4xX uOsonh+38yNoR9NdLilSYyxbH5VREDrmdwkI58+TiRJIeex5tuDIbwobf+1eksvBUSrD iuOJs5wlATUZf/xbNq0cRau5fKFM5HoktVBhry2vUx6Rz3wjyN9n1Icn5xeS6gm4KYGw OheEeGG65ksvkKAuI6qK5sHG1seoIxwkwEqSCha5/t5j5tHya2bk+rD8ooSVMy8qUQqd hoGg== X-Gm-Message-State: AOJu0YwFKaIObVeK5a0oKc+IlwKnJCyY8Ik45qWBf8EZe+NU9qp+L3mK 2XpgVQXF8KuPOWQ+8QalQEfprMwvHqC9g0BYwMzKQuZWjt2DmlrkLz/gBfplijh2+RcZZW3P2e4 b X-Google-Smtp-Source: AGHT+IFsXsuXl+00mO3vs6bnEI07yLJnBo+Ue7Qszr7Px4+ZLH7AGyObwzpvnP6H+I/zoaEFZeyXNQ== X-Received: by 2002:a17:907:f197:b0:a8a:85af:7ae8 with SMTP id a640c23a62f3a-a93a0325159mr172110966b.11.1727253786633; Wed, 25 Sep 2024 01:43:06 -0700 (PDT) From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Ross Lagerwall Subject: [PATCH v2 3/6] xen/livepatch: simplify and unify logic in prepare_payload() Date: Wed, 25 Sep 2024 10:42:36 +0200 Message-ID: <20240925084239.85649-4-roger.pau@citrix.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240925084239.85649-1-roger.pau@citrix.com> References: <20240925084239.85649-1-roger.pau@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1727253808198116600 The following sections: .note.gnu.build-id, .livepatch.xen_depends and .livepatch.depends are mandatory and ensured to be present by check_special_sections() before prepare_payload() is called. Simplify the logic in prepare_payload() by introducing a generic function to parse the sections that contain a buildid. Note the function assumes the buildid related section to always be present. No functional change intended. Signed-off-by: Roger Pau Monn=C3=A9 Reviewed-by: Andrew Cooper --- Changes since v1: - Rename. - Change order of assert. --- xen/common/livepatch.c | 110 +++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 60 deletions(-) diff --git a/xen/common/livepatch.c b/xen/common/livepatch.c index 87b3db03e26d..8e61083f23a7 100644 --- a/xen/common/livepatch.c +++ b/xen/common/livepatch.c @@ -470,6 +470,31 @@ static int xen_build_id_dep(const struct payload *payl= oad) return 0; } =20 +/* Parses build-id sections into the given destination. */ +static int parse_buildid(const struct livepatch_elf_sec *sec, + struct livepatch_build_id *id) +{ + const Elf_Note *n; + int rc; + + /* Presence of the sections is ensured by check_special_sections(). */ + ASSERT(sec); + + n =3D sec->load_addr; + + if ( sec->sec->sh_size <=3D sizeof(*n) ) + return -EINVAL; + + rc =3D xen_build_id_check(n, sec->sec->sh_size, &id->p, &id->len); + if ( rc ) + return rc; + + if ( !id->len || !id->p ) + return -EINVAL; + + return 0; +} + static int check_special_sections(const struct livepatch_elf *elf) { unsigned int i; @@ -641,11 +666,12 @@ static int prepare_payload(struct payload *payload, struct livepatch_elf *elf) { const struct livepatch_elf_sec *sec; + const struct payload *data; unsigned int i; struct livepatch_func *funcs; struct livepatch_func *f; struct virtual_region *region; - const Elf_Note *n; + int rc; =20 sec =3D livepatch_elf_sec_by_name(elf, ELF_LIVEPATCH_FUNC); if ( sec ) @@ -663,8 +689,6 @@ static int prepare_payload(struct payload *payload, =20 for ( i =3D 0; i < payload->nfuncs; i++ ) { - int rc; - f =3D &(funcs[i]); =20 if ( f->version !=3D LIVEPATCH_PAYLOAD_VERSION ) @@ -707,69 +731,35 @@ static int prepare_payload(struct payload *payload, LIVEPATCH_ASSIGN_SINGLE_HOOK(elf, payload->hooks.revert.action, ELF_LI= VEPATCH_REVERT_HOOK); LIVEPATCH_ASSIGN_SINGLE_HOOK(elf, payload->hooks.revert.post, ELF_LIVE= PATCH_POSTREVERT_HOOK); =20 - sec =3D livepatch_elf_sec_by_name(elf, ELF_BUILD_ID_NOTE); - if ( sec ) - { - const struct payload *data; - - n =3D sec->load_addr; - - if ( sec->sec->sh_size <=3D sizeof(*n) ) - return -EINVAL; - - if ( xen_build_id_check(n, sec->sec->sh_size, - &payload->id.p, &payload->id.len) ) - return -EINVAL; - - if ( !payload->id.len || !payload->id.p ) - return -EINVAL; + rc =3D parse_buildid(livepatch_elf_sec_by_name(elf, ELF_BUILD_ID_NOTE), + &payload->id); + if ( rc ) + return rc; =20 - /* Make sure it is not a duplicate. */ - list_for_each_entry ( data, &payload_list, list ) + /* Make sure it is not a duplicate. */ + list_for_each_entry ( data, &payload_list, list ) + { + /* No way _this_ payload is on the list. */ + ASSERT(data !=3D payload); + if ( data->id.len =3D=3D payload->id.len && + !memcmp(data->id.p, payload->id.p, data->id.len) ) { - /* No way _this_ payload is on the list. */ - ASSERT(data !=3D payload); - if ( data->id.len =3D=3D payload->id.len && - !memcmp(data->id.p, payload->id.p, data->id.len) ) - { - dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Already loaded as %s!= \n", - elf->name, data->name); - return -EEXIST; - } + dprintk(XENLOG_DEBUG, LIVEPATCH "%s: Already loaded as %s!\n", + elf->name, data->name); + return -EEXIST; } } =20 - sec =3D livepatch_elf_sec_by_name(elf, ELF_LIVEPATCH_DEPENDS); - if ( sec ) - { - n =3D sec->load_addr; - - if ( sec->sec->sh_size <=3D sizeof(*n) ) - return -EINVAL; - - if ( xen_build_id_check(n, sec->sec->sh_size, - &payload->dep.p, &payload->dep.len) ) - return -EINVAL; - - if ( !payload->dep.len || !payload->dep.p ) - return -EINVAL; - } - - sec =3D livepatch_elf_sec_by_name(elf, ELF_LIVEPATCH_XEN_DEPENDS); - if ( sec ) - { - n =3D sec->load_addr; - - if ( sec->sec->sh_size <=3D sizeof(*n) ) - return -EINVAL; - - if ( xen_build_id_check(n, sec->sec->sh_size, - &payload->xen_dep.p, &payload->xen_dep.len= ) ) - return -EINVAL; + rc =3D parse_buildid(livepatch_elf_sec_by_name(elf, ELF_LIVEPATCH_DEPE= NDS), + &payload->dep); + if ( rc ) + return rc; =20 - if ( !payload->xen_dep.len || !payload->xen_dep.p ) - return -EINVAL; - } + rc =3D parse_buildid(livepatch_elf_sec_by_name(elf, + ELF_LIVEPATCH_XEN_DEPENDS= ), + &payload->xen_dep); + if ( rc ) + return rc; =20 /* Setup the virtual region with proper data. */ region =3D &payload->region; --=20 2.46.0