From patchwork Fri Dec 11 19:04:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 1415245 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=sourceware.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=SqyyeAwZ; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Ct0bX18jSz9sTL for ; Sat, 12 Dec 2020 06:05:12 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D8EA43972C10; Fri, 11 Dec 2020 19:05:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D8EA43972C10 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1607713508; bh=9nUOyRVKwhXJuhmHAMztacDcB8fWe3/NpcFVTCpkTCM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=SqyyeAwZGWIKlru1s8iEUGXpevClSpYd0+oRmoRyVM4sLac1HTuMuMdDiRndk5k1E bzYMVHksXd0u3Ubv1ixMaQdTkmQoow2GiUCAHkTpaAKW41eCy7o3HYOlaSKlxVFlts vVQiQLfXsMVYAqM/GoTDXlzcayJNgc9p7foRt76k= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x843.google.com (mail-qt1-x843.google.com [IPv6:2607:f8b0:4864:20::843]) by sourceware.org (Postfix) with ESMTPS id 11E0C3972C01 for ; Fri, 11 Dec 2020 19:05:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 11E0C3972C01 Received: by mail-qt1-x843.google.com with SMTP id c14so7240364qtn.0 for ; Fri, 11 Dec 2020 11:05:05 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=9nUOyRVKwhXJuhmHAMztacDcB8fWe3/NpcFVTCpkTCM=; b=XYl/xZpU/gmmcQ4Q6ZvAgTKnoAWFygw+qI8abu1DDkOUnaVxY8euWxhoFcQIKJGFwA uHt+m75wVvX3ion++/UF3HyaDMYI39ENF47/k5sWPFHAcJ2Yxbg4JnTrqm8cCqWE4MZR z5fW3NNAIE8drcjNR6K2V7Q3YI52cd1CUXzwpkDzwnCgI2Ub0SZF9X1Am0AOA4vyt0dV wsKJNig8yxzxda/ND3B2ormge95QOd4UszlyHnJt3zWEU4YUW9dtYVzhq7BLxmhYhjmZ ewxoX+IQWcIpTWDNeUdSd3LvHTm0hS26gp+y02nkoIio3VlEMTUSSZcBu8dKMBs78AUQ XKBA== X-Gm-Message-State: AOAM5314XmC9vaihiTFTT2/G8qPXxCiCtevvJyzwY5JtTFVO7EBGjVl4 50buM+2bBDmfPnS2+6+OlGCYy9LOq4Pjjg== X-Google-Smtp-Source: ABdhPJyzN9hk6LrbB8cQgTZhydddrch0y8pDMlSvEfn1JC+RVBTwcpHiG0L3AuCeGbDOnd3+oKfpJg== X-Received: by 2002:a05:622a:346:: with SMTP id r6mr17150883qtw.299.1607713504308; Fri, 11 Dec 2020 11:05:04 -0800 (PST) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id q194sm8170089qka.102.2020.12.11.11.05.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Dec 2020 11:05:03 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH] posix: Fix return value of system if shell can not be executed [BZ #27053] Date: Fri, 11 Dec 2020 16:04:59 -0300 Message-Id: <20201211190459.1128661-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Cc: Michael Kerrisk Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" POSIX states that system returned code for failure to execute the shell shall be as if the shell had terminated using _exit(127). This behaviour was removed with 5fb7fc96350575. This issue should not be possible for pclose because if the shell can not be executed, popen would not return a valid FILE object. Other process execution failures (such as killed by a signal) would be returned through waitpid from pclose call. The new tst-system has an underlying issue once new containers tests are added that might issue the minimal container shell, since it changes the shell execution mode. It is not an issue now, since it is the only container tests for stdlib subforder, and I would like to avoid setting non parallel execution. Another possibility if concurrent container tests issues the minimum shell would be to add a advisory lock. Checked on x86_64-linux-gnu. --- stdlib/tst-system.c | 17 +++++++++++++++++ support/Makefile | 1 + support/xchmod.c | 30 ++++++++++++++++++++++++++++++ support/xunistd.h | 1 + sysdeps/posix/system.c | 4 ++++ 5 files changed, 53 insertions(+) create mode 100644 support/xchmod.c diff --git a/stdlib/tst-system.c b/stdlib/tst-system.c index eddea33f4c..f477aeb267 100644 --- a/stdlib/tst-system.c +++ b/stdlib/tst-system.c @@ -26,6 +26,7 @@ #include #include #include +#include static char *tmpdir; static long int namemax; @@ -138,6 +139,22 @@ do_test (void) support_capture_subprocess_check (&result, "system", 0, sc_allow_none); } + { + struct stat64 st; + xstat (_PATH_BSHELL, &st); + mode_t mode = st.st_mode; + xchmod (_PATH_BSHELL, mode & ~(S_IXUSR | S_IXGRP | S_IXOTH)); + + struct support_capture_subprocess result; + result = support_capture_subprocess (call_system, + &(struct args) { + "exit 1", 127, 0 + }); + support_capture_subprocess_check (&result, "system", 0, sc_allow_none); + + xchmod (_PATH_BSHELL, st.st_mode); + } + TEST_COMPARE (system (""), 0); return 0; diff --git a/support/Makefile b/support/Makefile index f5f59bf8d2..c89b16bbcb 100644 --- a/support/Makefile +++ b/support/Makefile @@ -90,6 +90,7 @@ libsupport-routines = \ xchroot \ xclock_gettime \ xclose \ + xchmod \ xconnect \ xcopy_file_range \ xdlfcn \ diff --git a/support/xchmod.c b/support/xchmod.c new file mode 100644 index 0000000000..5e403c7cc2 --- /dev/null +++ b/support/xchmod.c @@ -0,0 +1,30 @@ +/* chmod with error checking. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +#include + +void +xchmod (const char *pathname, mode_t mode) +{ + int r = chmod (pathname, mode); + if (r < 0) + FAIL_EXIT1 ("chmod (%s, %d): %m", pathname, mode); +} diff --git a/support/xunistd.h b/support/xunistd.h index b299db77ba..29b9a028b0 100644 --- a/support/xunistd.h +++ b/support/xunistd.h @@ -45,6 +45,7 @@ long xsysconf (int name); long long xlseek (int fd, long long offset, int whence); void xftruncate (int fd, long long length); void xsymlink (const char *target, const char *linkpath); +void xchmod (const char *pathname, mode_t mode); /* Equivalent of "mkdir -p". */ void xmkdirp (const char *, mode_t); diff --git a/sysdeps/posix/system.c b/sysdeps/posix/system.c index a03f478fc7..b86fc9b756 100644 --- a/sysdeps/posix/system.c +++ b/sysdeps/posix/system.c @@ -175,6 +175,10 @@ do_system (const char *line) __libc_cleanup_region_end (0); #endif } + else + /* POSIX states that failure to execute the shell should return + as if the shell had terminated using _exit(127). */ + status = W_EXITCODE (127, 0); DO_LOCK (); if (SUB_REF () == 0)