From patchwork Mon Dec 12 07:00:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Helge Deller X-Patchwork-Id: 1714811 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=gmx.de header.i=@gmx.de header.a=rsa-sha256 header.s=s31663417 header.b=RTqwYWGe; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4NVsy25qvjz240J for ; Mon, 12 Dec 2022 18:02:16 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p4coD-0002Bq-8h; Mon, 12 Dec 2022 02:01:05 -0500 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 1p4co7-0002AC-Vw for qemu-devel@nongnu.org; Mon, 12 Dec 2022 02:00:59 -0500 Received: from mout.gmx.net ([212.227.15.19]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p4co5-0005rn-Jq for qemu-devel@nongnu.org; Mon, 12 Dec 2022 02:00:59 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1670828447; bh=p0B55hDZ6iPP/bor3LBrzxxfGzPW1ikFpE21hNWWt1c=; h=X-UI-Sender-Class:Date:From:To:Subject; b=RTqwYWGe7U4ZOPqdXFuePpUKi5emglbJ/g8BnBTWkZkdXY58v78FC2rQ3i5KX81jI yvM0kLLuboBFZ7z7KNVBaxhMagiTVejPVRRPu9Q7xUEPYQPHOAG1rwx6D8M2IA8jLI 0U8Be0MQwFP7VCD5/YOUEpQhZR62I6WtL6/zY1U1dyt/Oat4GJmwO+W0pPYRn6ZUkf nlFr+NjklupL+ePX/HQ6ejAkM5MO53lIZspSIgU0owtqqCsei9dPnLN9+J82ZcnR1g 1qbCz/Lp6XXdj/o3EOcBfQcQXrVN+wjN/Lv1eEUnu1Pm2bJR9iJRw39JKKg4t6b7PY c1mtO5baqMmXg== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from p100 ([92.116.137.92]) by mail.gmx.net (mrgmx005 [212.227.17.190]) with ESMTPSA (Nemesis) id 1N7iCW-1oqgl316n4-014je0; Mon, 12 Dec 2022 08:00:47 +0100 Date: Mon, 12 Dec 2022 08:00:45 +0100 From: Helge Deller To: Laurent Vivier , Richard Henderson , Ilya Leoshkevich , Vitaly Buka , qemu-devel@nongnu.org Subject: [PATCH] linux-user: Add emulation for MADV_WIPEONFORK and MADV_KEEPONFORK in madvise() Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Provags-ID: V03:K1:4hcwySHZHkELp7Pq4zMyIE0oieNZk2j5/y/cUYaF9jDnPe8ZxkL 4yj6Hx7xG9pmRWhyDjtG20VpFsh8Yu4csm6YNbOv17dT9zgHEiK82oGCxituIMQKtRiQn7A w9mVtRN8k2qX3jHDLYcNi1Y2q9kZ260cwVQRJ9D/r3wriw5F9LsjHpzuN0Vd/XaNu01HaEZ VPlG8TH+MvC1E6kQ09xZw== UI-OutboundReport: notjunk:1;M01:P0:ZxpX6EmLGXU=;/gnkvwTsWUdSETWzt+DTZH/Dni3 O6sd1fyn+9QAZBvKJJ7d9JQTgehM91apPVY7JCooXyvRicGynzpj9P3lNitQYeeWVBqd8Ds66 yIk/Xk6iNn7CT94G0/6iXBv085WG11pQvYyua1vyJhsmFr0EwQVdnFgC65qa606AQKo4MZZ0L e+otz/haWIJShtjoWDYX7o9CkX6UiR5H39GCpQuq+47xpFcMwwIYMuGYQxWsTZ8osXvFBsgQ/ YdBVU2LYx1TgLGnG/jzKmooW/ILz6XJejvUzmTwZuuFcqOZEhoMdNaW/YN1zUuKAFgIPD1xz2 tWucrPTRH9rBBuOuveEnDUFZUa2uuczbW4kGd91DAubMeeoRDvMJ8NO4sEkRHiFcTjYOFR9cL 9qqVBx9jn9nRv6YAfBvrrkdvPIBREWRgIpk6PH8idsDh+C2pGJhOHQjzxGDnozwq049Sqe7TB 4H5hfHSCryE2cKzt8M9mOqQHMZ0+sx0dzV9C93wWTL69dymOx3uHEqFFfZS+3psbDOEHnY/QQ +NwVIeYBULvUMzjOAMo/t8P1Of3gD+3pK3wLPz185O+uJVrdFm2PrO9uH+Xk3fLr1PfuKb9TU /A/kZFQgqwhmfrsa6FjAWatmgMTxzdj4/0wigCKLcWf8Ruxrej0XeVe/2lDSj0IoSXhURs7z5 vxNA6aH1gbPeNcjlnN7E+A9jhuYDEVc+szv23XLfYXKdqiIUcMX1C/jdmrzoS/ij4zYy+vsYM MeHYEOx86o/qd9a/93IgYjOH1qCXtnoYOHbUDQux10gwLZQUNfmm0ke0Ho4MYLeehxGB8lDur B1Mh/dEOAoIpNTVzabj8pa+Uk3vzxLftQOCTNALiYGPs5SThZDkzROKwIblPZwCTOF4N9xuWb wWONyhJms97tH65T8jSNv2AODIHk7x+WPMdsPSBL1VnbRklQ6aVVKQQzorvsXOnKk9OYgT4iu DWY170pGQiPArQyDkM8Yy+b0HaM= Received-SPF: pass client-ip=212.227.15.19; envelope-from=deller@gmx.de; helo=mout.gmx.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Both parameters have a different value on the parisc platform, so first translate the target value into a host value for usage in the native madvise() syscall. Those parameters are often used by security sensitive applications (e.g. tor browser, boringssl, ...) which expect the call to return a proper return code on failure, so return -EINVAL if qemu fails to forward the syscall to the host OS. Tested with testcase of tor browser when running hppa-linux guest on x86-64 host. Signed-off-by: Helge Deller Acked-by: Ilya Leoshkevich diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 10f5079331..c75342108c 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -901,11 +901,25 @@ abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice) return -TARGET_EINVAL; } + /* Translate for some architectures which have different MADV_xxx values */ + switch (advice) { + case TARGET_MADV_DONTNEED: /* alpha */ + advice = MADV_DONTNEED; + break; + case TARGET_MADV_WIPEONFORK: /* parisc */ + advice = MADV_WIPEONFORK; + break; + case TARGET_MADV_KEEPONFORK: /* parisc */ + advice = MADV_KEEPONFORK; + break; + /* we do not care about the other MADV_xxx values yet */ + } + /* * A straight passthrough may not be safe because qemu sometimes turns * private file-backed mappings into anonymous mappings. * - * This is a hint, so ignoring and returning success is ok. + * For MADV_DONTNEED, which is a hint, ignoring and returning success is ok. * * This breaks MADV_DONTNEED, completely implementing which is quite * complicated. However, there is one low-hanging fruit: mappings that are @@ -913,11 +927,17 @@ abi_long target_madvise(abi_ulong start, abi_ulong len_in, int advice) * passthrough is safe, so do it. */ mmap_lock(); - if (advice == TARGET_MADV_DONTNEED && - can_passthrough_madv_dontneed(start, end)) { - ret = get_errno(madvise(g2h_untagged(start), len, MADV_DONTNEED)); - if (ret == 0) { - page_reset_target_data(start, start + len); + switch (advice) { + case MADV_WIPEONFORK: + case MADV_KEEPONFORK: + ret = -EINVAL; + /* fall through */ + case MADV_DONTNEED: + if (can_passthrough_madv_dontneed(start, end)) { + ret = get_errno(madvise(g2h_untagged(start), len, advice)); + if ((advice == MADV_DONTNEED) && (ret == 0)) { + page_reset_target_data(start, start + len); + } } } mmap_unlock();