Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp3252149pxb; Sat, 9 Oct 2021 05:53:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyrblm9wK3kqJl8e6fPkitgptnDPxcnJtdy1SczGpAwv0TJ9wZscE1iUuCWtOziw2NR99EM X-Received: by 2002:a63:1e5a:: with SMTP id p26mr9467108pgm.0.1633784033056; Sat, 09 Oct 2021 05:53:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633784033; cv=none; d=google.com; s=arc-20160816; b=0hyEXyDjss6/yhGlmJmWm1J4AtoFxovCatiovOavPHtc8I2MpZOvLMHWPvImYf9xgW dxmTNSkw+1m6wbdhPdJLPlT7jNojSY/DUnRCQ2IH6TW+KGfkJWYJWaVRUht3spSMln5b 7vw5aQ8TaxKy284XQgEdeeJFOWl72+kdbM1zhq6gnBHoCGJEQBUV7Bg8ltpL8zjOicMV hTs5aesTyaVoXDl6iFkQyTu57OoWlCbus8Nop22NNljoTJRXe9TQoo2uj+PHoLlNskLQ uVX19NHjks0Y7iApe/EdQCMTm/wdwSqk/6/am34OkReQ2B1MektJ/3jBemR6DA4iLQ7i Gipg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=975/0lKre2jbukZyrsOHoZ3KIJn08g0A4cVpQ8XD8kI=; b=aUOqGdLUtQIdzPGTA+6xS8f4l6YHSIUSgYW25D8zWbjh54fdJGJHvKXxGwH+aSJ16A sSqf9CSKCc2ihgdsnfkvD2hZWSj/Lenfd73HOGOM9W5Wz2tt5sJKlI2AbjD1O+oEaUsx Ltdijk09KkzH/+pOuq/Idc0HmY0aSYi3q7sUDgkY4SeUaFin2upRPcWk6NMVyUD18HSk wMXpbi8eeYY9CytN4PQkb92ttn8tlm0pbgFMVQ7mLcxckVp3KvDeBhpKRNlSBJABXKhi fEBSwKZfTCzdFtaqx+Zoe9whRfQ0hlNamEW5lqPGmiJyX9VN7oUy9NTo8lCDWfUp65uI q+oQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=hVcr+o13; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p1si2666607pls.420.2021.10.09.05.53.40; Sat, 09 Oct 2021 05:53:53 -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=@gmail.com header.s=20210112 header.b=hVcr+o13; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233137AbhJIMyF (ORCPT + 99 others); Sat, 9 Oct 2021 08:54:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37848 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231132AbhJIMyE (ORCPT ); Sat, 9 Oct 2021 08:54:04 -0400 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E786CC061570; Sat, 9 Oct 2021 05:52:07 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id d9so22944791edh.5; Sat, 09 Oct 2021 05:52:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=975/0lKre2jbukZyrsOHoZ3KIJn08g0A4cVpQ8XD8kI=; b=hVcr+o1302lP3dfkILDlV1SWMYeqJQl7pYfgbS3boQXLZweylwSzlGJGLLJwQNk8Kh CwL7ZYgMcu859cwij3WRju+M/6IBw1l5qVyv2sqs6vEeLyQuIkqRBOLAvpHG2flk3GUu tn0JJzxP25bxoKIzhRhoiWy9CUZiPDduVKRLOFHRfOkYUOyLurBQIqcI3eVAOA/JbRy2 URi2wY4/k/7uTuVYTbc7wFkTe2AuqPP9pdDjOKvn430JKAXbvj/JUkCfUmvxV47WBwOb Mj3LP/fA8TkfpmWrugYinFcFGixuwTX3+KlDePQpccjqsQkgWcVl0e5M7KhxZopGYHU0 f0Lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=975/0lKre2jbukZyrsOHoZ3KIJn08g0A4cVpQ8XD8kI=; b=58bRgh0YaApsf9ucmbdBUrwwnu1g2tFopow5ehNT7NW94QMrRzcmLItk2H/kFz3+mY XwF2sELqV8jQ1CXDwarwvo4La6X5Eujv9cCj9tAtqskh+bmyKYtKHgYdBoKWGSKRR8FH NTGZ78bUZ03A0lOfOGEui5NGDLWavOV0HNzQwqzHsc47Cqe43Bkmoyfs7a69XU0azDtY ZH6w+0Lk7CrjSJCksaEtw1sM3DSKNNcLt1/Jr2UoncN2VlC09vPYUGZa8xfqIRG3INHq mOYw5ZdlytbhIh9FNlt6eTGLY8/LVRL1sQPgNAVsj0qHMJnAhsFVUWxnKpPfeKnTh23/ q/lw== X-Gm-Message-State: AOAM532aokajDtWcxh8tk8E55SWI3Lws098OzbiEPI8hEr58pY9114MT RfIC26JzHIZE6YUjsIiGiQ== X-Received: by 2002:a17:906:7848:: with SMTP id p8mr11404695ejm.212.1633783926493; Sat, 09 Oct 2021 05:52:06 -0700 (PDT) Received: from localhost.localdomain ([46.53.249.82]) by smtp.gmail.com with ESMTPSA id l23sm967292ejn.15.2021.10.09.05.52.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 09 Oct 2021 05:52:06 -0700 (PDT) Date: Sat, 9 Oct 2021 15:52:02 +0300 From: Alexey Dobriyan To: Kees Cook Cc: Peter Zijlstra , Mark Rutland , Shuah Khan , linux-kselftest@vger.kernel.org, Josh Poimboeuf , Andrew Morton , Alexey Gladkov , jannh@google.com, vcaputo@pengaru.com, mingo@redhat.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, bristot@redhat.com, christian.brauner@ubuntu.com, amistry@google.com, Kenta.Tada@sony.com, legion@kernel.org, michael.weiss@aisec.fraunhofer.de, mhocko@suse.com, deller@gmx.de, zhengqi.arch@bytedance.com, me@tobin.cc, tycho@tycho.pizza, tglx@linutronix.de, bp@alien8.de, hpa@zytor.com, axboe@kernel.dk, metze@samba.org, laijs@linux.alibaba.com, luto@kernel.org, dave.hansen@linux.intel.com, ebiederm@xmission.com, ohoono.kwon@samsung.com, kaleshsingh@google.com, yifeifz2@illinois.edu, linux-arch@vger.kernel.org, vgupta@kernel.org, linux@armlinux.org.uk, will@kernel.org, guoren@kernel.org, bcain@codeaurora.org, monstr@monstr.eu, tsbogend@alpha.franken.de, nickhu@andestech.com, jonas@southpole.se, mpe@ellerman.id.au, paul.walmsley@sifive.com, hca@linux.ibm.com, ysato@users.sourceforge.jp, davem@davemloft.net, chris@zankel.net, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH] selftests: proc: Make sure wchan works when it exists Message-ID: References: <20211008235504.2957528-1-keescook@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20211008235504.2957528-1-keescook@chromium.org> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Oct 08, 2021 at 04:55:04PM -0700, Kees Cook wrote: > This makes sure that wchan contains a sensible symbol when a process is > blocked. > Specifically this calls the sleep() syscall, and expects the > architecture to have called schedule() from a function that has "sleep" > somewhere in its name. This exposes internal kernel symbol to userspace. Why would want to test that? Doing s/sleep/SLEEP/g doesn't change kernel but now the test is broken. > For example, on the architectures I tested > (x86_64, arm64, arm, mips, and powerpc) this is "hrtimer_nanosleep": > +/* > + * Make sure that wchan returns a reasonable symbol when blocked. > + */ Test should be "contains C identifier" then? > +int main(void) > +{ > + char buf[64]; > + pid_t child; > + int sync[2], fd; > + > + if (pipe(sync) < 0) > + perror_exit("pipe"); > + > + child = fork(); > + if (child < 0) > + perror_exit("fork"); > + if (child == 0) { > + /* Child */ > + if (close(sync[0]) < 0) > + perror_exit("child close sync[0]"); > + if (close(sync[1]) < 0) > + perror_exit("child close sync[1]"); Redundant close(). > + sleep(10); > + _exit(0); > + } > + /* Parent */ > + if (close(sync[1]) < 0) > + perror_exit("parent close sync[1]"); Redundant close(). > + if (read(sync[0], buf, 1) != 0) > + perror_exit("parent read sync[0]"); Racy if child is scheduled out after first close in the child. > + snprintf(buf, sizeof(buf), "/proc/%d/wchan", child); > + fd = open(buf, O_RDONLY); > + if (fd < 0) { > + if (errno == ENOENT) > + return 4; > + perror_exit(buf); > + } > + > + memset(buf, 0, sizeof(buf)); > + if (read(fd, buf, sizeof(buf) - 1) < 1) > + perror_exit(buf); > + if (strstr(buf, "sleep") == NULL) { > + fprintf(stderr, "FAIL: did not find 'sleep' in wchan '%s'\n", buf); > + return 1; > + } > + printf("ok: found 'sleep' in wchan '%s'\n", buf); > + > + if (kill(child, SIGKILL) < 0) > + perror_exit("kill"); > + if (waitpid(child, NULL, 0) != child) { > + fprintf(stderr, "waitpid: got the wrong child!?\n"); > + return 1; > + } > + > + return 0; > +}