From patchwork Wed Jan 4 11:20:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Giuseppe Scrivano X-Patchwork-Id: 38872 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp5090380wrt; Wed, 4 Jan 2023 03:23:46 -0800 (PST) X-Google-Smtp-Source: AMrXdXuB/BdLXCz3CRZ+TWJ/sJLYjhCMKF8ESO6G3VywMa5Wof+jmpjnKnaFneH5pF+Nf7ebIKDd X-Received: by 2002:a17:902:f30a:b0:192:5d9b:5881 with SMTP id c10-20020a170902f30a00b001925d9b5881mr35420116ple.31.1672831426140; Wed, 04 Jan 2023 03:23:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672831426; cv=none; d=google.com; s=arc-20160816; b=eptK+wX/1pbBShU1OyV6IFCNKWkEOyBEXXLrMrYx1j0Jmmtfaq5kyA26zUHCFg4kEi A9ZH+Qme4dOtVdqIKbu0u7QCQif3Jzz6DR2iN6nCdkhCpSgQpcskq1WOSMPkuSbW57T9 6gFHpnAKYMlfxH7Og9KFfJw9VYQn+brwGShPHkWm8wU5+QuIySnKpbIW0yLC/NfrUTTt aRAh5JU1FokbgsnJ30u8H+HGedPTSBtalkwYIQrxG5w3L5NJmwvwyKfF2HyFy+kSQghB mQY5FG8FaNKeLJu3kWECmDVweJA608XJq0o9BvgTxTDXyB3xYE98ynEBTtZ5NbtAa1VX xPNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=rZMl35g9o3N5e4d8Ph65UmYi5ZElld+YK7ZJYgVoFlI=; b=LEUy+41HlLcodzc6n7ka15+sYL2YnOhOXXplUXpZxvwfoOWQzLkwdOlyxvQSlKqa+0 GsHlsreTvebswOIy7ZfhrFHw0ExjJP6OjsHf0nvO6KK1ACHdMXtYKDBtGgE7auoMQfyX GwOafic9j0e8JaTS8Sgmh/8/ymal9WvOToi8CmY0L9uheBp6BW9b6RsTHsJRV6RpEZlq 571d98ks2Xz+MM5afMHkq/HHMipIZw6U5ksEP3/Xf++b1DUjZBrx1ko6ck1aVuOqPhRo ne2yXcA65RUfUKOQAzujKNnor9xynUnzsR5Q3N6lH3yWfEXDU2NMuPXx+laCJo963bEG t9aQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=NfxO1Kfz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a13-20020a1709027e4d00b00188dfcba393si33432786pln.437.2023.01.04.03.23.33; Wed, 04 Jan 2023 03:23:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=NfxO1Kfz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234854AbjADLW3 (ORCPT + 99 others); Wed, 4 Jan 2023 06:22:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234378AbjADLWU (ORCPT ); Wed, 4 Jan 2023 06:22:20 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47881E0DC for ; Wed, 4 Jan 2023 03:21:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672831290; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=rZMl35g9o3N5e4d8Ph65UmYi5ZElld+YK7ZJYgVoFlI=; b=NfxO1KfzROU1gyp/jWb227AV5pm0Qlpz6XOIX+nXGYcYObsWizPei4g9Xze9G9lWhVg4Vi ZeZcGr1jhKX2vAyrfFwOfXVhjkieKvW8camnrYkQpbNWu4FZc4DdHMyJjozN2bSgGd9/Tc 5hQ3YYD9rRusYLc6t+PAy1mlCEkZmQc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-400-a7eq5bBONJ6iGdEorSs_1w-1; Wed, 04 Jan 2023 06:21:25 -0500 X-MC-Unique: a7eq5bBONJ6iGdEorSs_1w-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A7E6580D181; Wed, 4 Jan 2023 11:21:24 +0000 (UTC) Received: from lithium.redhat.com (unknown [10.39.192.217]) by smtp.corp.redhat.com (Postfix) with ESMTP id 45384140EBF5; Wed, 4 Jan 2023 11:21:22 +0000 (UTC) From: Giuseppe Scrivano To: linux-kernel@vger.kernel.org Cc: keescook@chromium.org, bristot@redhat.com, ebiederm@xmission.com, brauner@kernel.org, cyphar@cyphar.com, viro@zeniv.linux.org.uk, alexl@redhat.com, peterz@infradead.org, gscrivan@redhat.com Subject: [RFC PATCH RESEND 1/2] exec: add PR_HIDE_SELF_EXE prctl Date: Wed, 4 Jan 2023 12:20:49 +0100 Message-Id: <20230104112050.402652-1-gscrivan@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754090885623339626?= X-GMAIL-MSGID: =?utf-8?q?1754090885623339626?= This patch adds a new prctl called PR_HIDE_SELF_EXE which allows processes to hide their own /proc/*/exe file. When this prctl is used, every access to /proc/*/exe for the calling process will fail with ENOENT. This is useful for preventing issues like CVE-2019-5736, where an attacker can gain host root access by overwriting the binary in OCI runtimes through file-descriptor mishandling in containers. The current fix for CVE-2019-5736 is to create a read-only copy or a bind-mount of the current executable, and then re-exec the current process. With the new prctl, the read-only copy or bind-mount copy is not needed anymore. Signed-off-by: Giuseppe Scrivano --- fs/exec.c | 1 + fs/proc/base.c | 8 +++++--- include/linux/sched.h | 5 +++++ include/uapi/linux/prctl.h | 3 +++ kernel/sys.c | 9 +++++++++ tools/include/uapi/linux/prctl.h | 3 +++ 6 files changed, 26 insertions(+), 3 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index ab913243a367..5a5dd964c3a3 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -1855,6 +1855,7 @@ static int bprm_execve(struct linux_binprm *bprm, /* execve succeeded */ current->fs->in_exec = 0; current->in_execve = 0; + task_clear_hide_self_exe(current); rseq_execve(current); acct_update_integrals(current); task_numa_free(current, false); diff --git a/fs/proc/base.c b/fs/proc/base.c index 9e479d7d202b..959968e2da0d 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -1723,19 +1723,21 @@ static int proc_exe_link(struct dentry *dentry, struct path *exe_path) { struct task_struct *task; struct file *exe_file; + long hide_self_exe; task = get_proc_task(d_inode(dentry)); if (!task) return -ENOENT; exe_file = get_task_exe_file(task); + hide_self_exe = task_hide_self_exe(task); put_task_struct(task); - if (exe_file) { + if (exe_file && !hide_self_exe) { *exe_path = exe_file->f_path; path_get(&exe_file->f_path); fput(exe_file); return 0; - } else - return -ENOENT; + } + return -ENOENT; } static const char *proc_pid_get_link(struct dentry *dentry, diff --git a/include/linux/sched.h b/include/linux/sched.h index 853d08f7562b..8db32d5fc285 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1790,6 +1790,7 @@ static __always_inline bool is_percpu_thread(void) #define PFA_SPEC_IB_DISABLE 5 /* Indirect branch speculation restricted */ #define PFA_SPEC_IB_FORCE_DISABLE 6 /* Indirect branch speculation permanently restricted */ #define PFA_SPEC_SSB_NOEXEC 7 /* Speculative Store Bypass clear on execve() */ +#define PFA_HIDE_SELF_EXE 8 /* Hide /proc/self/exe for the process */ #define TASK_PFA_TEST(name, func) \ static inline bool task_##func(struct task_struct *p) \ @@ -1832,6 +1833,10 @@ TASK_PFA_CLEAR(SPEC_IB_DISABLE, spec_ib_disable) TASK_PFA_TEST(SPEC_IB_FORCE_DISABLE, spec_ib_force_disable) TASK_PFA_SET(SPEC_IB_FORCE_DISABLE, spec_ib_force_disable) +TASK_PFA_TEST(HIDE_SELF_EXE, hide_self_exe) +TASK_PFA_SET(HIDE_SELF_EXE, hide_self_exe) +TASK_PFA_CLEAR(HIDE_SELF_EXE, hide_self_exe) + static inline void current_restore_flags(unsigned long orig_flags, unsigned long flags) { diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index a5e06dcbba13..f12f3df12468 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -284,4 +284,7 @@ struct prctl_mm_map { #define PR_SET_VMA 0x53564d41 # define PR_SET_VMA_ANON_NAME 0 +#define PR_SET_HIDE_SELF_EXE 65 +#define PR_GET_HIDE_SELF_EXE 66 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index 5fd54bf0e886..e992f1b72973 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2626,6 +2626,15 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, case PR_SET_VMA: error = prctl_set_vma(arg2, arg3, arg4, arg5); break; + case PR_SET_HIDE_SELF_EXE: + if (arg2 != 1 || arg3 || arg4 || arg5) + return -EINVAL; + task_set_hide_self_exe(current); + break; + case PR_GET_HIDE_SELF_EXE: + if (arg2 || arg3 || arg4 || arg5) + return -EINVAL; + return task_hide_self_exe(current) ? 1 : 0; default: error = -EINVAL; break; diff --git a/tools/include/uapi/linux/prctl.h b/tools/include/uapi/linux/prctl.h index a5e06dcbba13..f12f3df12468 100644 --- a/tools/include/uapi/linux/prctl.h +++ b/tools/include/uapi/linux/prctl.h @@ -284,4 +284,7 @@ struct prctl_mm_map { #define PR_SET_VMA 0x53564d41 # define PR_SET_VMA_ANON_NAME 0 +#define PR_SET_HIDE_SELF_EXE 65 +#define PR_GET_HIDE_SELF_EXE 66 + #endif /* _LINUX_PRCTL_H */ From patchwork Wed Jan 4 11:20:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Giuseppe Scrivano X-Patchwork-Id: 38871 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp5090323wrt; Wed, 4 Jan 2023 03:23:36 -0800 (PST) X-Google-Smtp-Source: AMrXdXuHXoO/Si9sVGdS6q3vZVrQNLY9aYH6zLnkTOwfOTJTRKfoG1JYFut2RjWV4ZUc9DLH2pFc X-Received: by 2002:a05:6a20:43ab:b0:a3:8512:54f7 with SMTP id i43-20020a056a2043ab00b000a3851254f7mr65506243pzl.29.1672831416363; Wed, 04 Jan 2023 03:23:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672831416; cv=none; d=google.com; s=arc-20160816; b=V/VNUdjNAZfV+eq8e4lKHLdfd/qWatp+TiYpE3ib+YH0RbFlPk5oocaww2/hVTaxuk r91K9odmGWovZiFBWFnvIOtZeHtE+IhPdMAMtKcl9xNATDxSxmaQ1k0QSSoKRUPczWkV ya/VuPLenqva1yVGSD5l/cGe/t166iko5oGGZ0yVOJ3UrWbBU++0Hb6XiaxTSYLNC6LN Yho4jwjfvQy8wW+x9HCLBb4rV0RLEyyCbLM+B+ZqLo53n7TXjryEZNGE+bVZABhZKwE0 Rddjhh+srGWnepXNSVYcvJL0NiNtl0vpxkTuqyBgjVN5mB/Z/rDC9VNwljlkC3YdNs8p Fm8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=q7FtdTeXSvj+IGog0ZsJoTNbFV/xfD/WPMUVexw05nU=; b=BD2F1z++bDxhwUvAUe6SJ3hEaHc9RjeJgQfgNc3s8uaUn3GZJ6mxWRiREkuZbEZf65 QQHSbEtKkw4xqMOqJR5DWUcwnL5wk75b3O8j/skSe/c3SnXvixxr1o+I3PN7LnYwF4Ue T0x3nxO6agmMRMxL080EEgzD9xRcKVVCNXcNkVpeKvK3Qqug0xuVeX1ZhIpX68x6g+P2 LbVFYwpe+kWP7dnEGekK7dpHOVYQqW+WgNxHVVwvRIcAibDP8InVa9V2xs5BeEs38USE XrOLghAzqCZp7uqmJCJPrx5QGdnFnikmnGY+w54cSFEenHrRTR7tyY5aIQucBUn1Gb9O tNVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QG02ijiB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a63-20020a639042000000b00461bf5f86fdsi35790197pge.403.2023.01.04.03.23.21; Wed, 04 Jan 2023 03:23:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QG02ijiB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234268AbjADLW0 (ORCPT + 99 others); Wed, 4 Jan 2023 06:22:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234706AbjADLWU (ORCPT ); Wed, 4 Jan 2023 06:22:20 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 58264E0FA for ; Wed, 4 Jan 2023 03:21:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672831290; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=q7FtdTeXSvj+IGog0ZsJoTNbFV/xfD/WPMUVexw05nU=; b=QG02ijiB+TH9HLUQ2zqLXPwq+fozTIfj329GSYBks+fYFNdNCoGE2glkB/2kzcoNi8VA8J Zt0FawNLhCc5Y48i4m+NRH6dmb4+IQ0c/2qB3ERQnBv9XcoZf8Dn0gPcjCMbc2EETDe9mI j+gQsCXcgk06NGRETebDUuGR3T4QkKY= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-613-iEjJYsd5Mn-gHUqutFj7nA-1; Wed, 04 Jan 2023 06:21:27 -0500 X-MC-Unique: iEjJYsd5Mn-gHUqutFj7nA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id EC5E48027F8; Wed, 4 Jan 2023 11:21:26 +0000 (UTC) Received: from lithium.redhat.com (unknown [10.39.192.217]) by smtp.corp.redhat.com (Postfix) with ESMTP id 14409140EBF5; Wed, 4 Jan 2023 11:21:24 +0000 (UTC) From: Giuseppe Scrivano To: linux-kernel@vger.kernel.org Cc: keescook@chromium.org, bristot@redhat.com, ebiederm@xmission.com, brauner@kernel.org, cyphar@cyphar.com, viro@zeniv.linux.org.uk, alexl@redhat.com, peterz@infradead.org, gscrivan@redhat.com Subject: [PATCH 2/2] selftests: add tests for prctl(SET_HIDE_SELF_EXE) Date: Wed, 4 Jan 2023 12:20:50 +0100 Message-Id: <20230104112050.402652-2-gscrivan@redhat.com> In-Reply-To: <20230104112050.402652-1-gscrivan@redhat.com> References: <20230104112050.402652-1-gscrivan@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754090875691788364?= X-GMAIL-MSGID: =?utf-8?q?1754090875691788364?= Signed-off-by: Giuseppe Scrivano --- tools/testing/selftests/prctl/Makefile | 2 +- tools/testing/selftests/prctl/hide-self-exe.c | 94 +++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/prctl/hide-self-exe.c diff --git a/tools/testing/selftests/prctl/Makefile b/tools/testing/selftests/prctl/Makefile index c7923b205222..024e107b26ec 100644 --- a/tools/testing/selftests/prctl/Makefile +++ b/tools/testing/selftests/prctl/Makefile @@ -5,7 +5,7 @@ ARCH ?= $(shell echo $(uname_M) | sed -e s/i.86/x86/ -e s/x86_64/x86/) ifeq ($(ARCH),x86) TEST_PROGS := disable-tsc-ctxt-sw-stress-test disable-tsc-on-off-stress-test \ - disable-tsc-test + disable-tsc-test hide-self-exe all: $(TEST_PROGS) include ../lib.mk diff --git a/tools/testing/selftests/prctl/hide-self-exe.c b/tools/testing/selftests/prctl/hide-self-exe.c new file mode 100644 index 000000000000..4085a473eadf --- /dev/null +++ b/tools/testing/selftests/prctl/hide-self-exe.c @@ -0,0 +1,94 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Tests for prctl(PR_GET_HIDE_SELF_EXE, ...) / prctl(PR_SET_HIDE_SELF_EXE, ...) + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#ifndef PR_SET_HIDE_SELF_EXE +# define PR_SET_HIDE_SELF_EXE 65 +# define PR_GET_HIDE_SELF_EXE 66 +#endif + +int main(void) +{ + int status; + pid_t pid; + int ret; + + ret = open("/proc/self/exe", O_RDONLY); + if (ret < 0) { + perror("open /proc/self/exe"); + exit(EXIT_FAILURE); + } + close(ret); + + ret = prctl(PR_GET_HIDE_SELF_EXE, 0, 0, 0, 0); + if (ret != 0) { + perror("prctl(PR_GET_HIDE_SELF_EXE)"); + exit(EXIT_FAILURE); + } + + ret = prctl(PR_SET_HIDE_SELF_EXE, 1, 0, 0, 0); + if (ret != 0) { + perror("prctl(PR_SET_HIDE_SELF_EXE)"); + exit(EXIT_FAILURE); + } + + /* check it doesn't fail a second time. */ + ret = prctl(PR_SET_HIDE_SELF_EXE, 1, 0, 0, 0); + if (ret != 0) { + perror("prctl(PR_SET_HIDE_SELF_EXE)"); + exit(EXIT_FAILURE); + } + + ret = prctl(PR_GET_HIDE_SELF_EXE, 0, 0, 0, 0); + if (ret != 1) { + perror("prctl(PR_GET_HIDE_SELF_EXE)"); + exit(EXIT_FAILURE); + } + + ret = open("/proc/self/exe", O_RDONLY); + if (ret >= 0 || errno != ENOENT) { + perror("open /proc/self/exe succeeded"); + exit(EXIT_FAILURE); + } + + pid = fork(); + if (pid < 0) { + perror("fork"); + exit(EXIT_FAILURE); + } + if (pid == 0) { + /* Verify that it is still unreachable after fork(). */ + ret = open("/proc/self/exe", O_RDONLY); + if (ret >= 0 || errno != ENOENT) + exit(EXIT_FAILURE); + close(ret); + + /* HIDE_SELF_EXE is cleared after execve. */ + ret = system("cat /proc/self/exe > /dev/null"); + exit(ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE); + } + if (waitpid(pid, &status, 0) != pid) { + perror("waitpid"); + exit(EXIT_FAILURE); + } + if (status != 0) { + perror("child failed"); + exit(EXIT_FAILURE); + } + exit(EXIT_SUCCESS); +} +