From patchwork Tue Mar 15 12:23:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Cervesato X-Patchwork-Id: 1605599 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=F6Srfm/B; dkim=fail reason="signature verification failed" header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=pSIiEiVU; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KHt0T3YyPz9sFq for ; Tue, 15 Mar 2022 23:25:29 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 3EB303C9319 for ; Tue, 15 Mar 2022 13:25:27 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-2.smtp.seeweb.it (in-2.smtp.seeweb.it [IPv6:2001:4b78:1:20::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384)) (No client certificate requested) by picard.linux.it (Postfix) with ESMTPS id 6158C3C933F for ; Tue, 15 Mar 2022 13:23:55 +0100 (CET) Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by in-2.smtp.seeweb.it (Postfix) with ESMTPS id ADF7260035B for ; Tue, 15 Mar 2022 13:23:54 +0100 (CET) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 2F1DC1F399; Tue, 15 Mar 2022 12:23:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1647347034; h=from:from:reply-to: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=6kqWcdEbMVFuORawxrYCz8KK/1Skcc2zeohJ6z76QJ4=; b=F6Srfm/BGg2IsoHXz+D5kCvsRq3UWeNBEBsN3P5Mh6IDeKiQflfihi8dUpc7WJPRprJDCe kuL6Dgv+KRbs+93Fm05kAXvLEyo11B3XpTlaQgfn17Ln22SHaF447QJMs4fI+iNy1shxk+ R5EZbnc4hiYwOo/ePCEtIq0LRnMFLHw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1647347034; h=from:from:reply-to: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=6kqWcdEbMVFuORawxrYCz8KK/1Skcc2zeohJ6z76QJ4=; b=pSIiEiVUxgtizVrXmx+0t47wswf3N1un9ktUPku82Ft6z9ap4eq0Lbnu4xW1S05kRNOy/F UqJMCKyx5Y79YGCQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 0535C13EDD; Tue, 15 Mar 2022 12:23:53 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id GMlhOlmFMGJ3UAAAMHmgww (envelope-from ); Tue, 15 Mar 2022 12:23:53 +0000 From: Andrea Cervesato To: ltp@lists.linux.it Date: Tue, 15 Mar 2022 13:23:45 +0100 Message-Id: <20220315122351.8556-3-andrea.cervesato@suse.de> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220315122351.8556-1-andrea.cervesato@suse.de> References: <20220315122351.8556-1-andrea.cervesato@suse.de> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.102.4 at in-2.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.1 required=7.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-2.smtp.seeweb.it Subject: [LTP] [PATCH v2 2/8] Rewrite userns02.c using new LTP API X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Signed-off-by: Andrea Cervesato Reviewed-by: Petr Vorel --- testcases/kernel/containers/userns/userns02.c | 139 ++++++++---------- 1 file changed, 59 insertions(+), 80 deletions(-) diff --git a/testcases/kernel/containers/userns/userns02.c b/testcases/kernel/containers/userns/userns02.c index ae49a1599..eaf29a1fd 100644 --- a/testcases/kernel/containers/userns/userns02.c +++ b/testcases/kernel/containers/userns/userns02.c @@ -1,74 +1,50 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) Huawei Technologies Co., Ltd., 2015 - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program 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 General Public License for more details. + * Copyright (C) 2022 SUSE LLC Andrea Cervesato */ -/* - * Verify that: - * The user ID and group ID, which are inside a container, can be modified - * by its parent process. +/*\ + * [Description] + * + * Verify that the user ID and group ID, which are inside a container, + * can be modified by its parent process. */ #define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include "userns_helper.h" -#include "test.h" - -char *TCID = "user_namespace2"; -int TST_TOTAL = 1; -static void cleanup(void) -{ - tst_rmdir(); -} +#include +#include +#include "common.h" +#include "tst_test.h" /* * child_fn1() - Inside a new user namespace */ static int child_fn1(void) { - int exit_val; int uid, gid; - TST_SAFE_CHECKPOINT_WAIT(NULL, 0); + TST_CHECKPOINT_WAIT(0); + uid = geteuid(); gid = getegid(); - if (uid == 100 && gid == 100) { - printf("Got expected uid and gid.\n"); - exit_val = 0; - } else { - printf("Got unexpected result of uid=%d gid=%d\n", uid, gid); - exit_val = 1; - } + if (uid == 100 && gid == 100) + tst_res(TPASS, "got expected uid and gid"); + else + tst_res(TFAIL, "got unexpected uid=%d gid=%d", uid, gid); - return exit_val; + return 0; } static void setup(void) { check_newuser(); - tst_tmpdir(); - TST_CHECKPOINT_INIT(NULL); } -int main(int argc, char *argv[]) +static void run(void) { - int lc; int childpid; int parentuid; int parentgid; @@ -76,42 +52,45 @@ int main(int argc, char *argv[]) char content[BUFSIZ]; int fd; - tst_parse_opts(argc, argv, NULL, NULL); - setup(); - - for (lc = 0; TEST_LOOPING(lc); lc++) { - tst_count = 0; - childpid = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, - (void *)child_fn1, NULL); - - if (childpid < 0) - tst_brkm(TFAIL | TERRNO, cleanup, "clone failed"); - - parentuid = geteuid(); - parentgid = getegid(); - sprintf(path, "/proc/%d/uid_map", childpid); - sprintf(content, "100 %d 1", parentuid); - fd = SAFE_OPEN(cleanup, path, O_WRONLY, 0644); - SAFE_WRITE(cleanup, 1, fd, content, strlen(content)); - SAFE_CLOSE(cleanup, fd); - - if (access("/proc/self/setgroups", F_OK) == 0) { - sprintf(path, "/proc/%d/setgroups", childpid); - fd = SAFE_OPEN(cleanup, path, O_WRONLY, 0644); - SAFE_WRITE(cleanup, 1, fd, "deny", 4); - SAFE_CLOSE(cleanup, fd); - } - - sprintf(path, "/proc/%d/gid_map", childpid); - sprintf(content, "100 %d 1", parentgid); - fd = SAFE_OPEN(cleanup, path, O_WRONLY, 0644); - SAFE_WRITE(cleanup, 1, fd, content, strlen(content)); - SAFE_CLOSE(cleanup, fd); - - TST_SAFE_CHECKPOINT_WAKE(cleanup, 0); - - tst_record_childstatus(cleanup, childpid); + childpid = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, (void *)child_fn1, NULL); + if (childpid < 0) + tst_brk(TBROK | TTERRNO, "clone failed"); + + parentuid = geteuid(); + parentgid = getegid(); + + sprintf(path, "/proc/%d/uid_map", childpid); + sprintf(content, "100 %d 1", parentuid); + + fd = SAFE_OPEN(path, O_WRONLY, 0644); + SAFE_WRITE(1, fd, content, strlen(content)); + SAFE_CLOSE(fd); + + if (access("/proc/self/setgroups", F_OK) == 0) { + sprintf(path, "/proc/%d/setgroups", childpid); + + fd = SAFE_OPEN(path, O_WRONLY, 0644); + SAFE_WRITE(1, fd, "deny", 4); + SAFE_CLOSE(fd); } - cleanup(); - tst_exit(); + + sprintf(path, "/proc/%d/gid_map", childpid); + sprintf(content, "100 %d 1", parentgid); + + fd = SAFE_OPEN(path, O_WRONLY, 0644); + SAFE_WRITE(1, fd, content, strlen(content)); + SAFE_CLOSE(fd); + + TST_CHECKPOINT_WAKE(0); } + +static struct tst_test test = { + .setup = setup, + .test_all = run, + .needs_root = 1, + .needs_checkpoints = 1, + .needs_kconfigs = (const char *[]) { + "CONFIG_USER_NS", + NULL, + }, +};