From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EFC29C18E5B for ; Tue, 10 Mar 2020 21:36:25 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id AD9D1222C3 for ; Tue, 10 Mar 2020 21:36:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="NXdmMClg" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AD9D1222C3 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 5E3166B0003; Tue, 10 Mar 2020 17:36:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 593736B0006; Tue, 10 Mar 2020 17:36:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 48A816B0007; Tue, 10 Mar 2020 17:36:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0200.hostedemail.com [216.40.44.200]) by kanga.kvack.org (Postfix) with ESMTP id 318EA6B0003 for ; Tue, 10 Mar 2020 17:36:25 -0400 (EDT) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 0C7288E72 for ; Tue, 10 Mar 2020 21:36:25 +0000 (UTC) X-FDA: 76580761530.30.gold33_1ae155797ba36 X-HE-Tag: gold33_1ae155797ba36 X-Filterd-Recvd-Size: 9270 Received: from mail-pf1-f193.google.com (mail-pf1-f193.google.com [209.85.210.193]) by imf38.hostedemail.com (Postfix) with ESMTP for ; Tue, 10 Mar 2020 21:36:24 +0000 (UTC) Received: by mail-pf1-f193.google.com with SMTP id w65so80526pfb.6 for ; Tue, 10 Mar 2020 14:36:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=NWf0TdeJvck0pCGGb01hva0XMggef5ubqIU9iaLUhUA=; b=NXdmMClgabWQioF4yjl5Jwd3G2+POPv9SbUzVwujJhh2o2BxjxxPsjmZx+RtR3ZTkT fid8mFGefGZRDOe9uPgT1YkjFUTL2YObjeqf7oy5iWafQQpc/Nr/eBX6Dqq2cvpJd3wt zEwIhTLVA6qRzzVmFGqNT3JOmI0cC8+6pR2cc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=NWf0TdeJvck0pCGGb01hva0XMggef5ubqIU9iaLUhUA=; b=cIK91zL4QZsjyDAujIWv+o9nnGUMZWtP1oy8v7qVX6I4gOrt3m/F+XaZ7x6QzRHm0K wxXXMkRxhx4MYPGmRVHFv3T7yKBpTbdmJRIYFENuEv3txJ8fEGFDfGdrZkSw6s1F4qRD wqOE68x7lMzli9fVlYS4G2uklMFyvpZo8NkSYuvtEW6fUk/0gFgZXCturIY+J8OBth4X OP7ayXqt7y2zeBhd2q7h7ky8j0DY4TFhXTPmC/m4HAltBXp8dE07noO/a5CIoX2bp28k lbLtWg4V018R2Ks8BZT8+PFmFrRwEav8NqCYIU+sLGu96vnXz60WEo6fb6DN2z7VY49o jx1w== X-Gm-Message-State: ANhLgQ1Q8VwDj+5fLurd1kD95Hc1wCSTL3dJ5G2SVKCL36cpnnifex3y 0lVhrz1a8rfizjBhl33Xb+I3Kg== X-Google-Smtp-Source: ADFU+vs/Uypr3q8a3tw+hkF+0Zw9ymbsnPXTwDAQkVznHvEHqBvv2ESuGLkB9ZYHjVG64HW7T+09ig== X-Received: by 2002:a65:44cd:: with SMTP id g13mr23586615pgs.365.1583876182960; Tue, 10 Mar 2020 14:36:22 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id g11sm20159100pfo.184.2020.03.10.14.36.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Mar 2020 14:36:21 -0700 (PDT) Date: Tue, 10 Mar 2020 14:36:20 -0700 From: Kees Cook To: Bernd Edlinger Cc: "Eric W. Biederman" , Christian Brauner , Jann Horn , Jonathan Corbet , Alexander Viro , Andrew Morton , Alexey Dobriyan , Thomas Gleixner , Oleg Nesterov , Frederic Weisbecker , Andrei Vagin , Ingo Molnar , "Peter Zijlstra (Intel)" , Yuyang Du , David Hildenbrand , Sebastian Andrzej Siewior , Anshuman Khandual , David Howells , James Morris , Greg Kroah-Hartman , Shakeel Butt , Jason Gunthorpe , Christian Kellner , Andrea Arcangeli , Aleksa Sarai , "Dmitry V. Levin" , "linux-doc@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "linux-fsdevel@vger.kernel.org" , "linux-mm@kvack.org" , "stable@vger.kernel.org" , "linux-api@vger.kernel.org" Subject: Re: [PATCH 2/4] selftests/ptrace: add test cases for dead-locks Message-ID: <202003101401.9A4FD3F@keescook> References: <878sk94eay.fsf@x220.int.ebiederm.org> <87r1y12yc7.fsf@x220.int.ebiederm.org> <87k13t2xpd.fsf@x220.int.ebiederm.org> <87d09l2x5n.fsf@x220.int.ebiederm.org> <871rq12vxu.fsf@x220.int.ebiederm.org> <877dzt1fnf.fsf@x220.int.ebiederm.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Tue, Mar 10, 2020 at 02:44:01PM +0100, Bernd Edlinger wrote: > 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 *discomfort noises* (see below) > > Signed-off-by: Bernd Edlinger > --- > 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 c0b7f89..2f1f532 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 This isn't the common solution in the kernel (the variable declaration would just be lifted out of the loop), but as it's selftest code, which does lots of special things ... I *guess* this is okay. > > -TEST_GEN_PROGS := get_syscall_info peeksiginfo > +TEST_GEN_PROGS := get_syscall_info peeksiginfo vmaccess I love having this deadlock test added to the selftests. I think I need to make an improvement to the test harness, though, as the failure mode right now just blows up after the 30 second timeout and leaves this deadlocked: $ ./vmaccess [==========] Running 2 tests from 1 test cases. [ RUN ] global.vmaccess Alarm clock $ ps PID TTY TIME CMD 2605 pts/0 00:00:00 bash 23360 pts/0 00:00:00 vmaccess 23361 pts/0 00:00:00 vmaccess 23363 pts/0 00:00:00 ps But that's mostly unrelated to this code. Reviewed-by: Kees Cook -Kees > > include ../lib.mk > diff --git a/tools/testing/selftests/ptrace/vmaccess.c b/tools/testing/selftests/ptrace/vmaccess.c > new file mode 100644 > index 0000000..4db327b > --- /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 > -- > 1.9.1 -- Kees Cook