From nobody Wed Feb 11 07:13:10 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1693351445; cv=none; d=zohomail.com; s=zohoarc; b=Pxb28mmi5RxTDSaDQGLSa0wZuqlLJq2X63jkYwv/aXBmeWduFs31JOyDn7EqlL1vC+fK5cJRo9LEGmWV9RJUC8oKxT3CnmrwCJ5CFJXQYVr/CxvRs5lIE3klMHmRph5b2bSr9Iq1gvFU0gban3gTxTvOuP/BQ0YXVZLwQwlL5ws= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1693351445; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=1QSy9ZU0qavvxc/B/Z2XKJEh1HZrVJBfcA9oJSYmMj8=; b=chuVy+Bw2isEs99grQAOu2BCZcVDpH3gTNnejaavRs9JDRpNii20FbTCbvGM44aocS/d+K/lN7i8bzD88EePuyCuuAtLLwE7Tnx2cFMEoxOiVX7KxXjN02SFQwlTHgnaAZj6yjyRRK9f+EYG+UK49nEjkZnwVG9PT1eont3KbdA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1693351445942119.95425339220037; Tue, 29 Aug 2023 16:24:05 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qb7oX-0002qd-1O; Tue, 29 Aug 2023 19:08:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qb6nH-0002Ng-3O for qemu-devel@nongnu.org; Tue, 29 Aug 2023 18:02:41 -0400 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qb6nE-00048Q-7U for qemu-devel@nongnu.org; Tue, 29 Aug 2023 18:02:38 -0400 Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-68a3082c771so202648b3a.0 for ; Tue, 29 Aug 2023 15:02:35 -0700 (PDT) Received: from stoup.. ([71.212.131.115]) by smtp.gmail.com with ESMTPSA id b28-20020a63715c000000b0056606274e54sm9538196pgn.31.2023.08.29.15.02.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Aug 2023 15:02:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1693346555; x=1693951355; darn=nongnu.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=1QSy9ZU0qavvxc/B/Z2XKJEh1HZrVJBfcA9oJSYmMj8=; b=ajxDbsK+v+9zNITLyGGUVb3KX/B0Q96RRcAcA6qyK0o9V4TmIH6OBplpkHG+96Plrf CGRiwvgUWcp/4SGtH5o3v204i9JshG7JtPYlZwocJFCv6Q9jQ8MJUOB1UZdw/AJSwYnf JDeP5+wgCEUyq+9BXt4Jlmwfb5yYKxMP4YY2ftMf+cOtWEdhcCv7XSzCtr9oB9uIE8rE Z+coLNhUvGdn3sHkGC7cKokfnv1Ahqn4uLWgeRsdPATGKeRLGn6tjYAUz2BDinwoz6UI dtGmqlmEfSR67ZtN/PuudOExYKhAg2WRassDz4l8otHVNrxHM/B5RHKAR3zpceDufW7C aayw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693346555; x=1693951355; 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=1QSy9ZU0qavvxc/B/Z2XKJEh1HZrVJBfcA9oJSYmMj8=; b=Buh/M74ajjsRTvvfHJ1BE0LusDyEpTZtmb0PS92ooIiVkXl8DufCF+i3SgGddT5uJ4 EWr0kD+y71E8yzBGNZpXqzK3Mo6hSOS/LHf9k4AVWfeR6tBojyaQ/OsLTRRK5LiJvA5/ 9j/d2X4d9BV4oHv5MKoZ3eejWuwE2PY6RVPt31dC8hVDxQiqMpm65whVj/IsljUcVpus SVZtc3FSvlkj/80IEuRJ6H/pvW3pTwY+cXTp3ME7qYTI2SJKO2+KxIaSxMTjjq95PmUu uu4q6tqAj6Yo4v5E4sU/atnn4yeazI634fn2NVv+omhm5GUfsFGHgtnptu+aXUJn020U X5EA== X-Gm-Message-State: AOJu0Yzyz5xP8YKnHHEvWBOh0DR2Jg6Ud4ktViGbivsaHtP5QJJ/fia2 NGQ8iNydiFn2rrMvXEmWH2lNWOB+nOCSqSF50iE= X-Google-Smtp-Source: AGHT+IFkRbMCfxnp0I0SHp4iAZBY3DHq85hxKQqrjaVsm7TvNBFbS0zLErfpd4wPiC1jdwtPLbDZwg== X-Received: by 2002:a05:6a20:3d1e:b0:125:3445:8af0 with SMTP id y30-20020a056a203d1e00b0012534458af0mr824205pzi.7.1693346554621; Tue, 29 Aug 2023 15:02:34 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org, laurent@vivier.eu Subject: [PATCH v5 05/20] linux-user: Use ImageSource in load_elf_image Date: Tue, 29 Aug 2023 15:02:13 -0700 Message-Id: <20230829220228.928506-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230829220228.928506-1-richard.henderson@linaro.org> References: <20230829220228.928506-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::434; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x434.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1693351448147100009 Content-Type: text/plain; charset="utf-8" Change parse_elf_properties as well, as the bprm_buf argument ties the two functions closely. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 128 +++++++++++++++++-------------------------- 1 file changed, 49 insertions(+), 79 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 4d3b25ed14..833c22da25 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2923,10 +2923,9 @@ static bool parse_elf_property(const uint32_t *data,= int *off, int datasz, } =20 /* Process NT_GNU_PROPERTY_TYPE_0. */ -static bool parse_elf_properties(int image_fd, +static bool parse_elf_properties(const ImageSource *src, struct image_info *info, const struct elf_phdr *phdr, - char bprm_buf[BPRM_BUF_SIZE], Error **errp) { union { @@ -2954,14 +2953,8 @@ static bool parse_elf_properties(int image_fd, return false; } =20 - if (phdr->p_offset + n <=3D BPRM_BUF_SIZE) { - memcpy(¬e, bprm_buf + phdr->p_offset, n); - } else { - ssize_t len =3D pread(image_fd, ¬e, n, phdr->p_offset); - if (len !=3D n) { - error_setg_errno(errp, errno, "Error reading file header"); - return false; - } + if (!imgsrc_read(¬e, phdr->p_offset, n, src, errp)) { + return false; } =20 /* @@ -3007,30 +3000,34 @@ static bool parse_elf_properties(int image_fd, } } =20 -/* Load an ELF image into the address space. +/** + * load_elf_image: Load an ELF image into the address space. + * @image_name: the filename of the image, to use in error messages. + * @src: the ImageSource from which to read. + * @info: info collected from the loaded image. + * @ehdr: the ELF header, not yet bswapped. + * @pinterp_name: record any PT_INTERP string found. + * + * On return: @info values will be filled in, as necessary or available. + */ =20 - IMAGE_NAME is the filename of the image, to use in error messages. - IMAGE_FD is the open file descriptor for the image. - - BPRM_BUF is a copy of the beginning of the file; this of course - contains the elf file header at offset 0. It is assumed that this - buffer is sufficiently aligned to present no problems to the host - in accessing data at aligned offsets within the buffer. - - On return: INFO values will be filled in, as necessary or available. */ - -static void load_elf_image(const char *image_name, int image_fd, +static void load_elf_image(const char *image_name, const ImageSource *src, struct image_info *info, struct elfhdr *ehdr, - char **pinterp_name, - char bprm_buf[BPRM_BUF_SIZE]) + char **pinterp_name) { - struct elf_phdr *phdr; + g_autofree struct elf_phdr *phdr =3D NULL; abi_ulong load_addr, load_bias, loaddr, hiaddr, error; - int i, retval, prot_exec; + int i, prot_exec; Error *err =3D NULL; =20 - /* First of all, some simple consistency checks */ - memcpy(ehdr, bprm_buf, sizeof(*ehdr)); + /* + * First of all, some simple consistency checks. + * Note that we rely on the bswapped ehdr staying in bprm_buf, + * for later use by load_elf_binary and create_elf_tables. + */ + if (!imgsrc_read(ehdr, 0, sizeof(*ehdr), src, &err)) { + goto exit_errmsg; + } if (!elf_check_ident(ehdr)) { error_setg(&err, "Invalid ELF image for this architecture"); goto exit_errmsg; @@ -3041,15 +3038,11 @@ static void load_elf_image(const char *image_name, = int image_fd, goto exit_errmsg; } =20 - i =3D ehdr->e_phnum * sizeof(struct elf_phdr); - if (ehdr->e_phoff + i <=3D BPRM_BUF_SIZE) { - phdr =3D (struct elf_phdr *)(bprm_buf + ehdr->e_phoff); - } else { - phdr =3D (struct elf_phdr *) alloca(i); - retval =3D pread(image_fd, phdr, i, ehdr->e_phoff); - if (retval !=3D i) { - goto exit_read; - } + phdr =3D imgsrc_read_alloc(ehdr->e_phoff, + ehdr->e_phnum * sizeof(struct elf_phdr), + src, &err); + if (phdr =3D=3D NULL) { + goto exit_errmsg; } bswap_phdr(phdr, ehdr->e_phnum); =20 @@ -3086,17 +3079,10 @@ static void load_elf_image(const char *image_name, = int image_fd, goto exit_errmsg; } =20 - interp_name =3D g_malloc(eppnt->p_filesz); - - if (eppnt->p_offset + eppnt->p_filesz <=3D BPRM_BUF_SIZE) { - memcpy(interp_name, bprm_buf + eppnt->p_offset, - eppnt->p_filesz); - } else { - retval =3D pread(image_fd, interp_name, eppnt->p_filesz, - eppnt->p_offset); - if (retval !=3D eppnt->p_filesz) { - goto exit_read; - } + interp_name =3D imgsrc_read_alloc(eppnt->p_offset, eppnt->p_fi= lesz, + src, &err); + if (interp_name =3D=3D NULL) { + goto exit_errmsg; } if (interp_name[eppnt->p_filesz - 1] !=3D 0) { error_setg(&err, "Invalid PT_INTERP entry"); @@ -3104,7 +3090,7 @@ static void load_elf_image(const char *image_name, in= t image_fd, } *pinterp_name =3D g_steal_pointer(&interp_name); } else if (eppnt->p_type =3D=3D PT_GNU_PROPERTY) { - if (!parse_elf_properties(image_fd, info, eppnt, bprm_buf, &er= r)) { + if (!parse_elf_properties(src, info, eppnt, &err)) { goto exit_errmsg; } } else if (eppnt->p_type =3D=3D PT_GNU_STACK) { @@ -3257,9 +3243,9 @@ static void load_elf_image(const char *image_name, in= t image_fd, * but no backing file segment. */ if (eppnt->p_filesz !=3D 0) { - error =3D target_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po, + error =3D imgsrc_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po, elf_prot, MAP_PRIVATE | MAP_FIXED, - image_fd, eppnt->p_offset - vaddr_po); + src, eppnt->p_offset - vaddr_po); if (error =3D=3D -1) { goto exit_mmap; } @@ -3291,20 +3277,11 @@ static void load_elf_image(const char *image_name, = int image_fd, #ifdef TARGET_MIPS } else if (eppnt->p_type =3D=3D PT_MIPS_ABIFLAGS) { Mips_elf_abiflags_v0 abiflags; - if (eppnt->p_filesz < sizeof(Mips_elf_abiflags_v0)) { - error_setg(&err, "Invalid PT_MIPS_ABIFLAGS entry"); + + if (!imgsrc_read(&abiflags, eppnt->p_offset, sizeof(abiflags), + src, &err)) { goto exit_errmsg; } - if (eppnt->p_offset + eppnt->p_filesz <=3D BPRM_BUF_SIZE) { - memcpy(&abiflags, bprm_buf + eppnt->p_offset, - sizeof(Mips_elf_abiflags_v0)); - } else { - retval =3D pread(image_fd, &abiflags, sizeof(Mips_elf_abif= lags_v0), - eppnt->p_offset); - if (retval !=3D sizeof(Mips_elf_abiflags_v0)) { - goto exit_read; - } - } bswap_mips_abiflags(&abiflags); info->fp_abi =3D abiflags.fp_abi; #endif @@ -3317,23 +3294,16 @@ static void load_elf_image(const char *image_name, = int image_fd, } =20 if (qemu_log_enabled()) { - load_symbols(ehdr, image_fd, load_bias); + load_symbols(ehdr, src->fd, load_bias); } =20 - debuginfo_report_elf(image_name, image_fd, load_bias); + debuginfo_report_elf(image_name, src->fd, load_bias); =20 mmap_unlock(); =20 - close(image_fd); + close(src->fd); return; =20 - exit_read: - if (retval >=3D 0) { - error_setg(&err, "Incomplete read of file header"); - } else { - error_setg_errno(&err, errno, "Error reading file header"); - } - goto exit_errmsg; exit_mmap: error_setg_errno(&err, errno, "Error mapping file"); goto exit_errmsg; @@ -3346,6 +3316,7 @@ static void load_elf_interp(const char *filename, str= uct image_info *info, char bprm_buf[BPRM_BUF_SIZE]) { struct elfhdr ehdr; + ImageSource src; int fd, retval; Error *err =3D NULL; =20 @@ -3363,11 +3334,11 @@ static void load_elf_interp(const char *filename, s= truct image_info *info, exit(-1); } =20 - if (retval < BPRM_BUF_SIZE) { - memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval); - } + src.fd =3D fd; + src.cache =3D bprm_buf; + src.cache_size =3D retval; =20 - load_elf_image(filename, fd, info, &ehdr, NULL, bprm_buf); + load_elf_image(filename, &src, info, &ehdr, NULL); } =20 static int symfind(const void *s0, const void *s1) @@ -3578,8 +3549,7 @@ int load_elf_binary(struct linux_binprm *bprm, struct= image_info *info) =20 info->start_mmap =3D (abi_ulong)ELF_START_MMAP; =20 - load_elf_image(bprm->filename, bprm->fd, info, - &ehdr, &elf_interpreter, bprm->buf); + load_elf_image(bprm->filename, &bprm->src, info, &ehdr, &elf_interpret= er); =20 /* Do this so that we can load the interpreter, if need be. We will change some of these later */ --=20 2.34.1