From patchwork Sat Jun 19 03:43:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 463851 Delivered-To: patch@linaro.org Received: by 2002:a05:6638:102:0:0:0:0 with SMTP id x2csp532771jao; Fri, 18 Jun 2021 20:44:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyVTSfILeNgqCLXFHb0DFgwup33wn1h8y/+heTTANv+QxfpTb6pqfV8678utgr7Fj76tl3b X-Received: by 2002:a05:6e02:5ac:: with SMTP id k12mr9667803ils.29.1624074270572; Fri, 18 Jun 2021 20:44:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624074270; cv=none; d=google.com; s=arc-20160816; b=A/stM7sVUp7iP8LFtP8uXW3nm3ly1Y7c0eBR6zxbV5++Nk+ZeSWICANArBNf5Ju/zY rTluXhnwfBydXTekLitKZjk3ih4A/hEGFe3AvcAewUAEYkcsCKgk80V0bZ/ZQb6MSCdQ a9+TiUsPgXaX5SLPsm5XkPtk8D2XXuEZ3jdy7Y0zvVnsZkKre/oo8AQ6F+dWfLBGcc5B sRn5Uqn9npGADj6bwkrlGSi+jWyPhBbtYYNIJYSX60ICeTqwWoboX/6U7lJ/xM/PL5eo H47okuvl47nZy82hRsafBXsYqZ4DMFCS9G69xNbGJ3vARF3PUKqiVO3VFnSlnCTIpkUK LoYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=dtXXZ8iOGwpaQfUOm6DW075wk6Tcr8F02y3NmtzbotE=; b=DD1N56MS4yTjbTa/FDU+mfWLLkq4ksAFtFn5a0xaidQvYHq2P6B86iw6URXS354G9P +D7AaP5NsrnZknOkKIxl30fMl5dut9w3GmOCoHHJdNJcbXmUyyxFv/47fmaOLLJNyoBg MoAqCGD3jLUnwVVt8ehg2JjziBwfq1dFzqL18vb4H25PsNhHIIGAFhohdORD6wgfmcZN OIEqV46grNAd4qptZ/30rx+fTI6uAe2vhf7x8rX/jlASdJNeyJf1RDR682Q1N8aQekxt N+Uergo3IHXbH25r5VpzOckQy3+vRETFm/FsF/eWa/vTrFRd3E0L09AUqt263UF2SYuL csOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=oma6t+J6; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id m1si3962918ilj.142.2021.06.18.20.44.30 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 18 Jun 2021 20:44:30 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=oma6t+J6; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:50836 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1luRuH-0000Hl-VQ for patch@linaro.org; Fri, 18 Jun 2021 23:44:29 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33058) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luRtV-00008J-UW for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:42 -0400 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]:33643) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1luRtP-00081j-10 for qemu-devel@nongnu.org; Fri, 18 Jun 2021 23:43:41 -0400 Received: by mail-pl1-x630.google.com with SMTP id f10so3546124plg.0 for ; Fri, 18 Jun 2021 20:43:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=dtXXZ8iOGwpaQfUOm6DW075wk6Tcr8F02y3NmtzbotE=; b=oma6t+J6HZwI1G5DffLzF9PdZihCwD5yizlAwk/JNOeRv87FMG5v7B0Z2dDbC9HE89 Z6xSQ7iqyjumfDXLJXzPcja39w00px12wHyMDdPSBZhDLv4/dHRQoKL8UCsHma1x+PWe fuHGI34Q7+flia9cmetY6Srz5n8cM78tuSECthmNFBgzX84vya9Bwzkwq0UCLsT1XEst WI+q0u32TD4lkOWVkwFQ9RHH55TOl1zpYqQpoNnHjMCHN9gqij31Od2Pi37OlJb/YRk4 40qs51WYJrtDIwHqZjM+ZMPomVzhRJRQnKVUa0dR/JXedD6BYYnFCCVDOq8z8XBEtz6/ ChQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=dtXXZ8iOGwpaQfUOm6DW075wk6Tcr8F02y3NmtzbotE=; b=ZxTnEh6NYxlyajyhfS7vttjsGcSu1MIe0FaNG5K68UwWBs2ONs0Jmr59bSdU5NRinZ SXw3YLZVuKMwvhpOL7q1H8ezKu4WAB6YfdXGsCEBrmImpE201salactNEcD14e9XUydm soj1n9zX3CkjHUGGWWoZSPu41KqcgAH8gQ5Rf5NM9EP3eJwbKFj5SSw6Kzj8yqtIFHdp NeRQqj+OBeiZHHaTa1whNr8BlNggxbw+noSWsWpXviuMrS3bv7fwBfav1hn5JmuzO609 0fSr4elzCqLBrGUPFGkvCt8bhLxaecg2k+H2SZywXL2U52F7pFkTY2+/jUgEV87HFIMU s7Eg== X-Gm-Message-State: AOAM532YThW1H56D94Jm4MytKYld5KDkwyrBH6IAv9hzbBzysmsW48gZ hhUALpvUVT2gSDYXonb+xWNxi1y02iK25A== X-Received: by 2002:a17:90b:393:: with SMTP id ga19mr14289824pjb.182.1624074213153; Fri, 18 Jun 2021 20:43:33 -0700 (PDT) Received: from localhost.localdomain ([71.212.149.176]) by smtp.gmail.com with ESMTPSA id z6sm10045154pgs.24.2021.06.18.20.43.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Jun 2021 20:43:32 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 04/12] linux-user: Do not clobber bprm_buf swapping ehdr Date: Fri, 18 Jun 2021 20:43:21 -0700 Message-Id: <20210619034329.532318-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210619034329.532318-1-richard.henderson@linaro.org> References: <20210619034329.532318-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::630; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x630.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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alex.bennee@linaro.org, laurent@vivier.eu Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Rearrange the allocation of storage for ehdr between load_elf_image and load_elf_binary. The same set of copies are done, but we don't modify bprm_buf, which will be important later. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) -- 2.25.1 Reviewed-by: Philippe Mathieu-Daudé diff --git a/linux-user/elfload.c b/linux-user/elfload.c index c0236a0b09..1f9a69703a 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2622,16 +2622,17 @@ static bool parse_elf_properties(int image_fd, On return: INFO values will be filled in, as necessary or available. */ static void load_elf_image(const char *image_name, int image_fd, - struct image_info *info, char **pinterp_name, + struct image_info *info, struct elfhdr *ehdr, + char **pinterp_name, char bprm_buf[BPRM_BUF_SIZE]) { - struct elfhdr *ehdr = (struct elfhdr *)bprm_buf; struct elf_phdr *phdr; abi_ulong load_addr, load_bias, loaddr, hiaddr, error; int i, retval, prot_exec; Error *err = NULL; /* First of all, some simple consistency checks */ + memcpy(ehdr, bprm_buf, sizeof(*ehdr)); if (!elf_check_ident(ehdr)) { error_setg(&err, "Invalid ELF image for this architecture"); goto exit_errmsg; @@ -2944,6 +2945,7 @@ static void load_elf_image(const char *image_name, int image_fd, static void load_elf_interp(const char *filename, struct image_info *info, char bprm_buf[BPRM_BUF_SIZE]) { + struct elfhdr ehdr; int fd, retval; Error *err = NULL; @@ -2965,7 +2967,7 @@ static void load_elf_interp(const char *filename, struct image_info *info, memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval); } - load_elf_image(filename, fd, info, NULL, bprm_buf); + load_elf_image(filename, fd, info, &ehdr, NULL, bprm_buf); } static int symfind(const void *s0, const void *s1) @@ -3157,8 +3159,14 @@ uint32_t get_elf_eflags(int fd) int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) { + /* + * We need a copy of the elf header for passing to create_elf_tables. + * We will have overwritten the original when we re-use bprm->buf + * while loading the interpreter. Allocate the storage for this now + * and let elf_load_image do any swapping that may be required. + */ + struct elfhdr ehdr; struct image_info interp_info; - struct elfhdr elf_ex; char *elf_interpreter = NULL; char *scratch; @@ -3170,12 +3178,7 @@ int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) info->start_mmap = (abi_ulong)ELF_START_MMAP; load_elf_image(bprm->filename, bprm->fd, info, - &elf_interpreter, bprm->buf); - - /* ??? We need a copy of the elf header for passing to create_elf_tables. - If we do nothing, we'll have overwritten this when we re-use bprm->buf - when we load the interpreter. */ - elf_ex = *(struct elfhdr *)bprm->buf; + &ehdr, &elf_interpreter, bprm->buf); /* Do this so that we can load the interpreter, if need be. We will change some of these later */ @@ -3245,7 +3248,7 @@ int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) target_mprotect(tramp_page, TARGET_PAGE_SIZE, PROT_READ | PROT_EXEC); } - bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex, + bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &ehdr, info, (elf_interpreter ? &interp_info : NULL)); info->start_stack = bprm->p;