From patchwork Tue Jun 28 17:38:30 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Vagin X-Patchwork-Id: 9203719 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id AA6576074E for ; Tue, 28 Jun 2016 17:40:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9C11028558 for ; Tue, 28 Jun 2016 17:40:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9019628606; Tue, 28 Jun 2016 17:40:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0E65328558 for ; Tue, 28 Jun 2016 17:40:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752515AbcF1RjM (ORCPT ); Tue, 28 Jun 2016 13:39:12 -0400 Received: from mail-pf0-f193.google.com ([209.85.192.193]:34323 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752434AbcF1RjK (ORCPT ); Tue, 28 Jun 2016 13:39:10 -0400 Received: by mail-pf0-f193.google.com with SMTP id 66so2268063pfy.1; Tue, 28 Jun 2016 10:39:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=jeE5fuNyhWPhDPOPqmttAyptzJqhcyXygKU6A8HefjQ=; b=ycYkLA018bcuhTMDJQQV8upDye2TZLmKchrkz0E//+70hLYhUuzSEZyZWI9XDm7sw2 JAS9L3X3e7aHIM+e0weliVk2R9jdB4j2M0e/ruVhmbuKSELeYXkDlL2crcqs5w8TpbJc 7uEmsAEFmxsZv3/gUZrybp/a95FhHwPVqKIAIEOvzu39rR2w527pX2yjLr1LZoYjvLl3 c7VuIKNFhhjXXCDHpyIq1S5XMOjLGTbAkN6Ee6SKLgs8iVAa5MleGSOvLkR4pgX34T69 UygWGwBwtff6bl6Y99F5aiE/NinGzmqf/bXcHHVKwjymL1YM501fSP9GCThg4zswcQNy G3Hg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=jeE5fuNyhWPhDPOPqmttAyptzJqhcyXygKU6A8HefjQ=; b=NRTd7+UEXnl9+OnbKhLvMiOy5YuIvwkj11DBoUeYKqzPn9OFTlDJq7k/Z36gLBlMnW Ne2F9vBK/rAq45Hsu7vyD0+aN3ynTrqeNJsZk43TV5lIFzrx/xxkDOx7UEzPs02IAIpL 9J/Xqcij85Ohd3Fh8UHdIW1Ll7RFrIInM5/GSAQikVw7eFyYBxYBOItniLRVnPFACvBS m4mMpYOyTZgjw8U4K7o/rxi1WsBBUh3SPmbhgCAJc98RDCDTow21GSfBl/NAeeys5VkH HnkAFxFaP7zqPTTVYYJQn3lcutJcIAuKXO/2Xg046EQrNZh1T2KklB9v1dF7MgElAKxU BzIw== X-Gm-Message-State: ALyK8tKgHGNzQoB7oZaGxwO1b5QHZT1kSqH73rqcwLf0X144I++DCu53HJTCiyauxnmXLg== X-Received: by 10.98.61.131 with SMTP id x3mr3863214pfj.115.1467135549266; Tue, 28 Jun 2016 10:39:09 -0700 (PDT) Received: from laptop.vz.com ([162.246.95.100]) by smtp.gmail.com with ESMTPSA id 69sm458114pfc.90.2016.06.28.10.39.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 Jun 2016 10:39:08 -0700 (PDT) From: Andrey Vagin To: linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, criu@openvz.org, containers@lists.linux-foundation.org, Andrey Vagin , Alexander Viro , "Eric W. Biederman" , Arnd Bergmann , "J. Bruce Fields" , Miklos Szeredi , NeilBrown , Shuah Khan Subject: [PATCH 3/3] selftests: check O_ATROOT and AT_FDROOT flags Date: Tue, 28 Jun 2016 10:38:30 -0700 Message-Id: <1467135510-2564-4-git-send-email-avagin@openvz.org> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1467135510-2564-1-git-send-email-avagin@openvz.org> References: <1467135510-2564-1-git-send-email-avagin@openvz.org> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP These flags mean that the first argument "dirfd" of *at syscall-s set as root for the current operation. With these flags absolute symlinks have to be resolved relative to dirfd. This test create a file and an absolute symlink on it, which can be resoled only if a proper root is set. Signed-off-by: Andrey Vagin --- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/lookup/.gitignore | 1 + tools/testing/selftests/lookup/Makefile | 8 +++ tools/testing/selftests/lookup/lookup_at_root.c | 71 +++++++++++++++++++++++++ tools/testing/selftests/lookup/run.sh | 14 +++++ 5 files changed, 95 insertions(+) create mode 100644 tools/testing/selftests/lookup/.gitignore create mode 100644 tools/testing/selftests/lookup/Makefile create mode 100644 tools/testing/selftests/lookup/lookup_at_root.c create mode 100755 tools/testing/selftests/lookup/run.sh diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index ff9e5f2..72555c8 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -9,6 +9,7 @@ TARGETS += futex TARGETS += ipc TARGETS += kcmp TARGETS += lib +TARGETS += lookup TARGETS += membarrier TARGETS += memfd TARGETS += memory-hotplug diff --git a/tools/testing/selftests/lookup/.gitignore b/tools/testing/selftests/lookup/.gitignore new file mode 100644 index 0000000..c9a963f --- /dev/null +++ b/tools/testing/selftests/lookup/.gitignore @@ -0,0 +1 @@ +lookup_at_root diff --git a/tools/testing/selftests/lookup/Makefile b/tools/testing/selftests/lookup/Makefile new file mode 100644 index 0000000..042b26f --- /dev/null +++ b/tools/testing/selftests/lookup/Makefile @@ -0,0 +1,8 @@ +TEST_PROGS := run.sh + +all: lookup_at_root + +clean: + $(RM) lookup_at_root +include ../lib.mk + diff --git a/tools/testing/selftests/lookup/lookup_at_root.c b/tools/testing/selftests/lookup/lookup_at_root.c new file mode 100644 index 0000000..6723dc8 --- /dev/null +++ b/tools/testing/selftests/lookup/lookup_at_root.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define pr_err(fmt, ...) \ + ({ \ + fprintf(stderr, "%s:%d:" fmt ": %m\n", \ + __func__, __LINE__, ##__VA_ARGS__); \ + 1; \ + }) + +#ifndef O_ATROOT +#define O_ATROOT 040000000 /* dfd is a root */ +#endif +#ifndef AT_FDROOT +#define AT_FDROOT 0x2000 /* Resolve a path as if dirfd is root */ +#endif + +int main(int argc, char **argv) +{ + struct stat st; + int fd, dfd; + char path[PATH_MAX]; + + dfd = open(argv[1], O_RDONLY); + if (dfd < 0) + return pr_err("open"); + + snprintf(path, sizeof(path), "%s/test", argv[1]); + if (mkdir(path, 755)) + return pr_err("mkdir"); + + if (symlinkat("/test", dfd, "./test.link")) + return pr_err("symlinkat"); + + fd = openat(dfd, "test.link", O_RDONLY | O_ATROOT); + if (fd < 0) + return pr_err("open"); + + if (fchdir(dfd)) + return pr_err("fchdir"); + + fd = openat(AT_FDCWD, "test.link", O_RDONLY | O_ATROOT); + if (fd < 0) + return pr_err("open"); + close(fd); + + fd = openat(AT_FDCWD, "/test.link", O_RDONLY | O_ATROOT); + if (fd < 0) + return pr_err("open"); + close(fd); + + if (fstatat(AT_FDCWD, "test.link", &st, AT_FDROOT)) + return pr_err("fstatat"); + if (fstatat(dfd, "test.link", &st, AT_FDROOT)) + return pr_err("fstatat"); + if (mknodat(dfd, "./test/test.file", 0644 | S_IFREG, 0)) + return pr_err("mknod"); + if (linkat(dfd, "./test.link/test.file", + dfd, "./test.link/test.file.link", AT_FDROOT)) + return pr_err("linkat"); + if (unlinkat(dfd, "./test.link/test.file.link", AT_FDROOT)) + return pr_err("unlinkat"); + + return 0; +} diff --git a/tools/testing/selftests/lookup/run.sh b/tools/testing/selftests/lookup/run.sh new file mode 100755 index 0000000..86cc51b --- /dev/null +++ b/tools/testing/selftests/lookup/run.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +set -e + +test_dir=`mktemp -d /tmp/lookup_test.XXXXXX` +mount -t tmpfs lookup_at_root $test_dir + +ret=0 +./lookup_at_root $test_dir || ret=$? + +umount $test_dir +rmdir $test_dir + +exit $ret