Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp853218pxk; Thu, 17 Sep 2020 19:07:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz2GsEcfH2vLhb9QDl72vyOpxEZA2bBaz6rvB5hgihcuyf0PaoN4OevPrheoDfCJrYT9bOR X-Received: by 2002:a17:906:ae50:: with SMTP id lf16mr32856866ejb.343.1600394877619; Thu, 17 Sep 2020 19:07:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600394877; cv=none; d=google.com; s=arc-20160816; b=jS9wjK5MvgSygC/xkmXip0+AccOCMsadaAp68oQWnpyXt9dON8JMn2D8UgD1k1pGox mMmn616zDpT69lRZ/1lUEN3KXBfZe1RSGq4MkR7FQ7uw+DVM9JCXYNIpTSRKyUD2dI9N ZLq3wD7jNy9nN+TMgGk9YllQlpi7l9bXZ92gkwQhRe1yatCufli+yI/UTYf1IsHJilWO wY0ZEKwi6T6bVnCGr7sGkUVYP1B41M7nPnbAoPEDc/L+B9ZKZugDXkFZZG7Z1h5ob/ef NEzJS+WHXXs+lraCQdScKO3dNkei4i/mAAznbd+SWFG6FELG5DS+DgmKUA8Fx39R2vFI uYxQ== 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=nwbt6TFann3YOaz/j5GshLTWvkjP84Dv8Fx4gFE4GGU=; b=hydq/oPP+xf/CtW3wOIXBwaIw2pQC+HdQ+8bR/gw9x9QhjITRKooHHPXvot+aits9b GdKebiXYj1N7JHE2CzUetZAlhEzth1a/TzW48NAk4XnVoLQoAIMNqiCB1spu7ncobqPi 5T/a/od64wVMwoekMruRyGFsLYtJGCicKFICQpZf/IhJoBuyDbIJmPit2QtOxzBKd5+1 HHMCc8A/EMmSVdcwTI5X7XazKZNgUZ69FReEw6+Nh8jkZiQsjTwrs5+4B300Dw+EpQIA PmkJFUnAFZg/lSZt68oaAOJ05SjPrshZtGs5TkgJi2lhL19MoDE/EYGvh77j6oOiA5gN dt7w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=o1q1maf5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f2si1117443edn.26.2020.09.17.19.07.34; Thu, 17 Sep 2020 19:07:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=o1q1maf5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727550AbgIRCFa (ORCPT + 99 others); Thu, 17 Sep 2020 22:05:30 -0400 Received: from mail.kernel.org ([198.145.29.99]:53478 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726109AbgIRCFR (ORCPT ); Thu, 17 Sep 2020 22:05:17 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id AC6582388E; Fri, 18 Sep 2020 02:05:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600394716; bh=u1qOtWnMXAlI04ZQS3g+oAR6V6XOrFj1hlQQ0sO9zp0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=o1q1maf5js0nsQtet2uG7EdAJJXKd1gsNKK+Gb+9tuep/iYyz7kDh+p7hJ8hBSwaT G3vNDMryjiKKR2YgiVIkO6QD8gW9hDeuGK7ZlVjJZaoMEAH+Cl3gmpXhyuV8vLYnTQ w9ERsnDe2/mbDFxioL8csH337wrS77sw1pZ0WtCE= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Bernd Edlinger , Kees Cook , "Eric W . Biederman" , Sasha Levin , linux-api@vger.kernel.org Subject: [PATCH AUTOSEL 5.4 200/330] selftests/ptrace: add test cases for dead-locks Date: Thu, 17 Sep 2020 21:59:00 -0400 Message-Id: <20200918020110.2063155-200-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918020110.2063155-1-sashal@kernel.org> References: <20200918020110.2063155-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bernd Edlinger [ Upstream commit 2de4e82318c7f9d34f4b08599a612cd4cd10bf0b ] This adds test cases for ptrace deadlocks. Additionally fixes a compile problem in get_syscall_info.c, observed with gcc-4.8.4: get_syscall_info.c: In function 'get_syscall_info': get_syscall_info.c:93:3: error: 'for' loop initial declarations are only allowed in C99 mode for (unsigned int i = 0; i < ARRAY_SIZE(args); ++i) { ^ get_syscall_info.c:93:3: note: use option -std=c99 or -std=gnu99 to compile your code Signed-off-by: Bernd Edlinger Reviewed-by: Kees Cook Signed-off-by: Eric W. Biederman Signed-off-by: Sasha Levin --- tools/testing/selftests/ptrace/Makefile | 4 +- tools/testing/selftests/ptrace/vmaccess.c | 86 +++++++++++++++++++++++ 2 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/ptrace/vmaccess.c diff --git a/tools/testing/selftests/ptrace/Makefile b/tools/testing/selftests/ptrace/Makefile index c0b7f89f09300..2f1f532c39dbc 100644 --- a/tools/testing/selftests/ptrace/Makefile +++ b/tools/testing/selftests/ptrace/Makefile @@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only -CFLAGS += -iquote../../../../include/uapi -Wall +CFLAGS += -std=c99 -pthread -iquote../../../../include/uapi -Wall -TEST_GEN_PROGS := get_syscall_info peeksiginfo +TEST_GEN_PROGS := get_syscall_info peeksiginfo vmaccess include ../lib.mk diff --git a/tools/testing/selftests/ptrace/vmaccess.c b/tools/testing/selftests/ptrace/vmaccess.c new file mode 100644 index 0000000000000..4db327b445862 --- /dev/null +++ b/tools/testing/selftests/ptrace/vmaccess.c @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (c) 2020 Bernd Edlinger + * All rights reserved. + * + * Check whether /proc/$pid/mem can be accessed without causing deadlocks + * when de_thread is blocked with ->cred_guard_mutex held. + */ + +#include "../kselftest_harness.h" +#include +#include +#include +#include +#include +#include + +static void *thread(void *arg) +{ + ptrace(PTRACE_TRACEME, 0, 0L, 0L); + return NULL; +} + +TEST(vmaccess) +{ + int f, pid = fork(); + char mm[64]; + + if (!pid) { + pthread_t pt; + + pthread_create(&pt, NULL, thread, NULL); + pthread_join(pt, NULL); + execlp("true", "true", NULL); + } + + sleep(1); + sprintf(mm, "/proc/%d/mem", pid); + f = open(mm, O_RDONLY); + ASSERT_GE(f, 0); + close(f); + f = kill(pid, SIGCONT); + ASSERT_EQ(f, 0); +} + +TEST(attach) +{ + int s, k, pid = fork(); + + if (!pid) { + pthread_t pt; + + pthread_create(&pt, NULL, thread, NULL); + pthread_join(pt, NULL); + execlp("sleep", "sleep", "2", NULL); + } + + sleep(1); + k = ptrace(PTRACE_ATTACH, pid, 0L, 0L); + ASSERT_EQ(errno, EAGAIN); + ASSERT_EQ(k, -1); + k = waitpid(-1, &s, WNOHANG); + ASSERT_NE(k, -1); + ASSERT_NE(k, 0); + ASSERT_NE(k, pid); + ASSERT_EQ(WIFEXITED(s), 1); + ASSERT_EQ(WEXITSTATUS(s), 0); + sleep(1); + k = ptrace(PTRACE_ATTACH, pid, 0L, 0L); + ASSERT_EQ(k, 0); + k = waitpid(-1, &s, 0); + ASSERT_EQ(k, pid); + ASSERT_EQ(WIFSTOPPED(s), 1); + ASSERT_EQ(WSTOPSIG(s), SIGSTOP); + k = ptrace(PTRACE_DETACH, pid, 0L, 0L); + ASSERT_EQ(k, 0); + k = waitpid(-1, &s, 0); + ASSERT_EQ(k, pid); + ASSERT_EQ(WIFEXITED(s), 1); + ASSERT_EQ(WEXITSTATUS(s), 0); + k = waitpid(-1, NULL, 0); + ASSERT_EQ(k, -1); + ASSERT_EQ(errno, ECHILD); +} + +TEST_HARNESS_MAIN -- 2.25.1