From patchwork Thu Sep 8 19:58:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?G=C3=BCnther_Noack?= X-Patchwork-Id: 12970627 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C2CA8C38145 for ; Thu, 8 Sep 2022 19:59:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229707AbiIHT7G (ORCPT ); Thu, 8 Sep 2022 15:59:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229941AbiIHT6r (ORCPT ); Thu, 8 Sep 2022 15:58:47 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97206112129; Thu, 8 Sep 2022 12:58:16 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id t7so22977210wrm.10; Thu, 08 Sep 2022 12:58:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=X5JtbaRKpQNt66W3vP0r+JXCBaH0/CYt+RJ9N8favnA=; b=XqGD5FARU1AX3e65qWyzZ0kAAbGFkJkv/CKaCEs9R5PSmyrgp/DQQo42yTcURlx8KS GraapwkoTOY6N+H/COiSRQ7t9BNMUSTwXYXIlrDhhMgtOuR+D1okLrkY90sss5JmbRIP Un4TZMuf9O4Wj3xXAYlUB71TK+CUA3pv1eXTXblP271t8VdjVN8XPm6dkVtDQQTXZ0TT UqXGGc2xSqK9a7nvSWVJJGoVGADkHqpRbHg/YEgHLD6hatcAYfkhkyD3H7XNTEJT4JE/ IOiwFscbdkoJ2pqiONB/NjUFgvb4IJxzNb154A1hwlE14SGX5P6b5n6OuFcb6eu8yIAX 2jZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=X5JtbaRKpQNt66W3vP0r+JXCBaH0/CYt+RJ9N8favnA=; b=yKFtYII4VY13D/UkrX5Ieai3UVSRAh9yHj9nO8Pnyk5pCAVAxsAy8D1kAz+2K5ADiI SXOt366YdsCvikX+0Txf7pQ1FfoFZSo9ZNWVtrXRIgnbHpecW+A5UGxY/8kM+yCkXspl XoEauozPfiLYapJiewVO/pPWJku2m3pCkqHll6+mLvBEBJvBiRDV9w5jK2LLU82SbtAM ebqFNzP+0vb993hAi9DztPciDSMomBDaXcChRkgbaIOhTihfD9f94KWomDyrZngHwJEm bYNYSFiHjYZrTQ77korcuJKHGA2cXs/Pu7/MNTf7aBixq8o0bRtjRFsRAs0d4W0RYixc kGwA== X-Gm-Message-State: ACgBeo21eYaI9nVhy/LZzmUh3D31c9kATUrmuBC1BoQMZUFi/VRVDJfg T8Y/cBRREUvJYcm+wxa72u+tuX34qkw= X-Google-Smtp-Source: AA6agR7V7rQS62oTQNFWTcpUV7z4pId9u1HM0xRVLalbjQpXRqien6PtGCntq8ioFUwbs2c9SYxVjw== X-Received: by 2002:a5d:5984:0:b0:227:1a6c:a507 with SMTP id n4-20020a5d5984000000b002271a6ca507mr6484057wri.292.1662667091361; Thu, 08 Sep 2022 12:58:11 -0700 (PDT) Received: from nuc.i.gnoack.org ([2a02:168:633b:1:1e69:7aff:fe05:97e6]) by smtp.gmail.com with ESMTPSA id a22-20020a05600c2d5600b003a541d893desm3360682wmg.38.2022.09.08.12.58.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Sep 2022 12:58:11 -0700 (PDT) From: =?utf-8?q?G=C3=BCnther_Noack?= To: linux-security-module@vger.kernel.org Cc: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= , James Morris , Paul Moore , "Serge E . Hallyn" , linux-fsdevel@vger.kernel.org, Konstantin Meskhidze , =?utf-8?q?G=C3=BCnt?= =?utf-8?q?her_Noack?= Subject: [PATCH v6 1/5] security: create file_truncate hook from path_truncate hook Date: Thu, 8 Sep 2022 21:58:01 +0200 Message-Id: <20220908195805.128252-2-gnoack3000@gmail.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220908195805.128252-1-gnoack3000@gmail.com> References: <20220908195805.128252-1-gnoack3000@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: Like path_truncate, the file_truncate hook also restricts file truncation, but is called in the cases where truncation is attempted on an already-opened file. This is required in a subsequent commit to handle ftruncate() operations differently to truncate() operations. Signed-off-by: Günther Noack Acked-by: Tetsuo Handa Acked-by: John Johansen --- fs/namei.c | 6 +++--- fs/open.c | 4 ++-- include/linux/lsm_hook_defs.h | 1 + include/linux/security.h | 6 ++++++ security/apparmor/lsm.c | 6 ++++++ security/security.c | 5 +++++ security/tomoyo/tomoyo.c | 13 +++++++++++++ 7 files changed, 36 insertions(+), 5 deletions(-) diff --git a/fs/namei.c b/fs/namei.c index 53b4bc094db2..52105873d1f8 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -53,8 +53,8 @@ * The new code replaces the old recursive symlink resolution with * an iterative one (in case of non-nested symlink chains). It does * this with calls to _follow_link(). - * As a side effect, dir_namei(), _namei() and follow_link() are now - * replaced with a single function lookup_dentry() that can handle all + * As a side effect, dir_namei(), _namei() and follow_link() are now + * replaced with a single function lookup_dentry() that can handle all * the special cases of the former code. * * With the new dcache, the pathname is stored at each inode, at least as @@ -3211,7 +3211,7 @@ static int handle_truncate(struct user_namespace *mnt_userns, struct file *filp) if (error) return error; - error = security_path_truncate(path); + error = security_file_truncate(filp); if (!error) { error = do_truncate(mnt_userns, path->dentry, 0, ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, diff --git a/fs/open.c b/fs/open.c index 8a813fa5ca56..0831433e493a 100644 --- a/fs/open.c +++ b/fs/open.c @@ -188,7 +188,7 @@ long do_sys_ftruncate(unsigned int fd, loff_t length, int small) if (IS_APPEND(file_inode(f.file))) goto out_putf; sb_start_write(inode->i_sb); - error = security_path_truncate(&f.file->f_path); + error = security_file_truncate(f.file); if (!error) error = do_truncate(file_mnt_user_ns(f.file), dentry, length, ATTR_MTIME | ATTR_CTIME, f.file); @@ -1271,7 +1271,7 @@ struct file *filp_open(const char *filename, int flags, umode_t mode) { struct filename *name = getname_kernel(filename); struct file *file = ERR_CAST(name); - + if (!IS_ERR(name)) { file = file_open_name(name, flags, mode); putname(name); diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h index 60fff133c0b1..dee35ab253ba 100644 --- a/include/linux/lsm_hook_defs.h +++ b/include/linux/lsm_hook_defs.h @@ -177,6 +177,7 @@ LSM_HOOK(int, 0, file_send_sigiotask, struct task_struct *tsk, struct fown_struct *fown, int sig) LSM_HOOK(int, 0, file_receive, struct file *file) LSM_HOOK(int, 0, file_open, struct file *file) +LSM_HOOK(int, 0, file_truncate, struct file *file) LSM_HOOK(int, 0, task_alloc, struct task_struct *task, unsigned long clone_flags) LSM_HOOK(void, LSM_RET_VOID, task_free, struct task_struct *task) diff --git a/include/linux/security.h b/include/linux/security.h index 7bd0c490703d..f80b23382dd9 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -394,6 +394,7 @@ int security_file_send_sigiotask(struct task_struct *tsk, struct fown_struct *fown, int sig); int security_file_receive(struct file *file); int security_file_open(struct file *file); +int security_file_truncate(struct file *file); int security_task_alloc(struct task_struct *task, unsigned long clone_flags); void security_task_free(struct task_struct *task); int security_cred_alloc_blank(struct cred *cred, gfp_t gfp); @@ -1011,6 +1012,11 @@ static inline int security_file_open(struct file *file) return 0; } +static inline int security_file_truncate(struct file *file) +{ + return 0; +} + static inline int security_task_alloc(struct task_struct *task, unsigned long clone_flags) { diff --git a/security/apparmor/lsm.c b/security/apparmor/lsm.c index e29cade7b662..98ecb7f221b8 100644 --- a/security/apparmor/lsm.c +++ b/security/apparmor/lsm.c @@ -329,6 +329,11 @@ static int apparmor_path_truncate(const struct path *path) return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR); } +static int apparmor_file_truncate(struct file *file) +{ + return apparmor_path_truncate(&file->f_path); +} + static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry, const char *old_name) { @@ -1232,6 +1237,7 @@ static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = { LSM_HOOK_INIT(mmap_file, apparmor_mmap_file), LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect), LSM_HOOK_INIT(file_lock, apparmor_file_lock), + LSM_HOOK_INIT(file_truncate, apparmor_file_truncate), LSM_HOOK_INIT(getprocattr, apparmor_getprocattr), LSM_HOOK_INIT(setprocattr, apparmor_setprocattr), diff --git a/security/security.c b/security/security.c index 4b95de24bc8d..e491120c48ba 100644 --- a/security/security.c +++ b/security/security.c @@ -1210,6 +1210,11 @@ int security_path_truncate(const struct path *path) return call_int_hook(path_truncate, 0, path); } +int security_file_truncate(struct file *file) +{ + return call_int_hook(file_truncate, 0, file); +} + int security_path_chmod(const struct path *path, umode_t mode) { if (unlikely(IS_PRIVATE(d_backing_inode(path->dentry)))) diff --git a/security/tomoyo/tomoyo.c b/security/tomoyo/tomoyo.c index 71e82d855ebf..af04a7b7eb28 100644 --- a/security/tomoyo/tomoyo.c +++ b/security/tomoyo/tomoyo.c @@ -134,6 +134,18 @@ static int tomoyo_path_truncate(const struct path *path) return tomoyo_path_perm(TOMOYO_TYPE_TRUNCATE, path, NULL); } +/** + * tomoyo_file_truncate - Target for security_file_truncate(). + * + * @file: Pointer to "struct file". + * + * Returns 0 on success, negative value otherwise. + */ +static int tomoyo_file_truncate(struct file *file) +{ + return tomoyo_path_truncate(&file->f_path); +} + /** * tomoyo_path_unlink - Target for security_path_unlink(). * @@ -545,6 +557,7 @@ static struct security_hook_list tomoyo_hooks[] __lsm_ro_after_init = { LSM_HOOK_INIT(bprm_check_security, tomoyo_bprm_check_security), LSM_HOOK_INIT(file_fcntl, tomoyo_file_fcntl), LSM_HOOK_INIT(file_open, tomoyo_file_open), + LSM_HOOK_INIT(file_truncate, tomoyo_file_truncate), LSM_HOOK_INIT(path_truncate, tomoyo_path_truncate), LSM_HOOK_INIT(path_unlink, tomoyo_path_unlink), LSM_HOOK_INIT(path_mkdir, tomoyo_path_mkdir), From patchwork Thu Sep 8 19:58:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?G=C3=BCnther_Noack?= X-Patchwork-Id: 12970626 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ADF70C6FA86 for ; Thu, 8 Sep 2022 19:59:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229695AbiIHT7F (ORCPT ); Thu, 8 Sep 2022 15:59:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229940AbiIHT6r (ORCPT ); Thu, 8 Sep 2022 15:58:47 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05737113C72; Thu, 8 Sep 2022 12:58:17 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id az24-20020a05600c601800b003a842e4983cso2720387wmb.0; Thu, 08 Sep 2022 12:58:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=VBifi/eGKzGwDx/g0RTxlUK19IZkULja/108w9EnqoQ=; b=XPWn7BrLDJCwHXO8RV/S9I+SW38KPQGlq6F+KpGmvBlrJp4XwhkJj3IS/wQywjJtHe bfxS9yiDi3Cubpagw+QOUhvfyLXvxqpY9d8gQ1AdR734+6J4wvNtL2JSItAU4qyl9MGB qlcXOzSyOUGSmU8XiuQVdusYTlXG4h4g6RVaxh24Xj27Mb0mf6kGpGm8DbAUyppTJ1F0 Axt2Lz7lK1RCMk3ctkgvBwNN1V4qlf0Ei7XmPTfE47bveU4jNyn8/U+jL0+fKoRqRePS zDB4plRg+z7Qiqhkb+pWiTGq98ZhfZ8a4o50bb2Wiz8PHnuYv/C5D8TawioMycI0qG6y 69Pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=VBifi/eGKzGwDx/g0RTxlUK19IZkULja/108w9EnqoQ=; b=1LX3Df+xXbg2Pw+JK0Mm5ry/2voqidY9k0G1ZQQk06tqyCrDRTGya4pJovK0e4m3/V GN6BAqOqU2n7DePz960tPWKHmr0RpY2fuJa5dCdAkzPGKP/UF/gbn9sdQ+V6Akz3EPT9 k0VxnZE706J1PX2CrsjkAvlGc3sWXRW/PUyaS9l3pZiR/Wu0V2PJAmY7B6QowNt+ViCp t9PPRgd8pjh0lwOymbUGZ1JrK/ojDX739Vwgns9CYpR1D90/7xw0KWRc+yHnabVwJZWJ 7UZOJU7x6r/vFt0yZLVoi1kBtmsYRtZwfiU8kgJehz6NzJVdj27X0rC5hdWw59xk5DRu UTdQ== X-Gm-Message-State: ACgBeo0oeaUDAAaiRWX+IN5o+1pjYSPe1O0RIW2wDfPLX3olYSf9BKV4 +4nJE2xC9kV/FU+TGV4vFExydfMoDLQ= X-Google-Smtp-Source: AA6agR4KeXh1Ykqorp1/nXhdjNxiQhPsDdv026UmDo6uGM8OYapxdh3/9VMAeipUegEA2lBYyQA1Wg== X-Received: by 2002:a05:600c:34c2:b0:3a5:d2f5:9d02 with SMTP id d2-20020a05600c34c200b003a5d2f59d02mr3521646wmq.153.1662667092119; Thu, 08 Sep 2022 12:58:12 -0700 (PDT) Received: from nuc.i.gnoack.org ([2a02:168:633b:1:1e69:7aff:fe05:97e6]) by smtp.gmail.com with ESMTPSA id a22-20020a05600c2d5600b003a541d893desm3360682wmg.38.2022.09.08.12.58.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Sep 2022 12:58:11 -0700 (PDT) From: =?utf-8?q?G=C3=BCnther_Noack?= To: linux-security-module@vger.kernel.org Cc: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= , James Morris , Paul Moore , "Serge E . Hallyn" , linux-fsdevel@vger.kernel.org, Konstantin Meskhidze , =?utf-8?q?G=C3=BCnt?= =?utf-8?q?her_Noack?= Subject: [PATCH v6 2/5] landlock: Support file truncation Date: Thu, 8 Sep 2022 21:58:02 +0200 Message-Id: <20220908195805.128252-3-gnoack3000@gmail.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220908195805.128252-1-gnoack3000@gmail.com> References: <20220908195805.128252-1-gnoack3000@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: Introduce the LANDLOCK_ACCESS_FS_TRUNCATE flag for file truncation. This flag hooks into the path_truncate LSM hook and covers file truncation using truncate(2), ftruncate(2), open(2) with O_TRUNC, as well as creat(). This change also increments the Landlock ABI version, updates corresponding selftests, and updates code documentation to document the flag. The following operations are restricted: open(): requires the LANDLOCK_ACCESS_FS_TRUNCATE right if a file gets implicitly truncated as part of the open() (e.g. using O_TRUNC). Notable special cases: * open(..., O_RDONLY|O_TRUNC) can truncate files as well in Linux * open() with O_TRUNC does *not* need the TRUNCATE right when it creates a new file. truncate() (on a path): requires the LANDLOCK_ACCESS_FS_TRUNCATE right. ftruncate() (on a file): requires that the file had the TRUNCATE right when it was previously opened. Signed-off-by: Günther Noack --- include/uapi/linux/landlock.h | 18 ++-- security/landlock/fs.c | 88 +++++++++++++++++++- security/landlock/fs.h | 18 ++++ security/landlock/limits.h | 2 +- security/landlock/setup.c | 1 + security/landlock/syscalls.c | 2 +- tools/testing/selftests/landlock/base_test.c | 2 +- tools/testing/selftests/landlock/fs_test.c | 7 +- 8 files changed, 124 insertions(+), 14 deletions(-) diff --git a/include/uapi/linux/landlock.h b/include/uapi/linux/landlock.h index 23df4e0e8ace..8c0124c5cbe6 100644 --- a/include/uapi/linux/landlock.h +++ b/include/uapi/linux/landlock.h @@ -95,8 +95,16 @@ struct landlock_path_beneath_attr { * A file can only receive these access rights: * * - %LANDLOCK_ACCESS_FS_EXECUTE: Execute a file. - * - %LANDLOCK_ACCESS_FS_WRITE_FILE: Open a file with write access. + * - %LANDLOCK_ACCESS_FS_WRITE_FILE: Open a file with write access. Note that + * you might additionally need the `LANDLOCK_ACCESS_FS_TRUNCATE` right in + * order to overwrite files with :manpage:`open(2)` using `O_TRUNC` or + * :manpage:`creat(2)`. * - %LANDLOCK_ACCESS_FS_READ_FILE: Open a file with read access. + * - %LANDLOCK_ACCESS_FS_TRUNCATE: Truncate a file with :manpage:`truncate(2)`, + * :manpage:`ftruncate(2)`, :manpage:`creat(2)`, or :manpage:`open(2)` with + * `O_TRUNC`. The right to truncate a file gets carried along with an opened + * file descriptor for the purpose of :manpage:`ftruncate(2)`. This access + * right is available since the third version of the Landlock ABI. * * A directory can receive access rights related to files or directories. The * following access right is applied to the directory itself, and the @@ -139,10 +147,9 @@ struct landlock_path_beneath_attr { * * It is currently not possible to restrict some file-related actions * accessible through these syscall families: :manpage:`chdir(2)`, - * :manpage:`truncate(2)`, :manpage:`stat(2)`, :manpage:`flock(2)`, - * :manpage:`chmod(2)`, :manpage:`chown(2)`, :manpage:`setxattr(2)`, - * :manpage:`utime(2)`, :manpage:`ioctl(2)`, :manpage:`fcntl(2)`, - * :manpage:`access(2)`. + * :manpage:`stat(2)`, :manpage:`flock(2)`, :manpage:`chmod(2)`, + * :manpage:`chown(2)`, :manpage:`setxattr(2)`, :manpage:`utime(2)`, + * :manpage:`ioctl(2)`, :manpage:`fcntl(2)`, :manpage:`access(2)`. * Future Landlock evolutions will enable to restrict them. */ /* clang-format off */ @@ -160,6 +167,7 @@ struct landlock_path_beneath_attr { #define LANDLOCK_ACCESS_FS_MAKE_BLOCK (1ULL << 11) #define LANDLOCK_ACCESS_FS_MAKE_SYM (1ULL << 12) #define LANDLOCK_ACCESS_FS_REFER (1ULL << 13) +#define LANDLOCK_ACCESS_FS_TRUNCATE (1ULL << 14) /* clang-format on */ #endif /* _UAPI_LINUX_LANDLOCK_H */ diff --git a/security/landlock/fs.c b/security/landlock/fs.c index a9dbd99d9ee7..1b546edf69a6 100644 --- a/security/landlock/fs.c +++ b/security/landlock/fs.c @@ -146,7 +146,8 @@ static struct landlock_object *get_inode_object(struct inode *const inode) #define ACCESS_FILE ( \ LANDLOCK_ACCESS_FS_EXECUTE | \ LANDLOCK_ACCESS_FS_WRITE_FILE | \ - LANDLOCK_ACCESS_FS_READ_FILE) + LANDLOCK_ACCESS_FS_READ_FILE | \ + LANDLOCK_ACCESS_FS_TRUNCATE) /* clang-format on */ /* @@ -761,6 +762,47 @@ static bool collect_domain_accesses( return ret; } +/** + * get_path_access_rights - Returns the subset of rights in access_request + * which are permitted for the given path. + * + * @domain: The domain that defines the current restrictions. + * @path: The path to get access rights for. + * @access_request: The rights we are interested in. + * + * Returns: The access mask of the rights that are permitted on the given path, + * which are also a subset of access_request (to save some calculation time). + */ +static inline access_mask_t +get_path_access_rights(const struct landlock_ruleset *const domain, + const struct path *const path, + access_mask_t access_request) +{ + layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS] = {}; + unsigned long access_bit; + unsigned long access_req; + + init_layer_masks(domain, access_request, &layer_masks); + if (!check_access_path_dual(domain, path, access_request, &layer_masks, + NULL, 0, NULL, NULL)) { + /* + * Return immediately for successful accesses and for cases + * where everything is permitted because the path belongs to an + * internal filesystem. + */ + return access_request; + } + + access_req = access_request; + for_each_set_bit(access_bit, &access_req, ARRAY_SIZE(layer_masks)) { + if (layer_masks[access_bit]) { + /* If any layer vetoed the access right, remove it. */ + access_request &= ~BIT_ULL(access_bit); + } + } + return access_request; +} + /** * current_check_refer_path - Check if a rename or link action is allowed * @@ -1142,6 +1184,11 @@ static int hook_path_rmdir(const struct path *const dir, return current_check_access_path(dir, LANDLOCK_ACCESS_FS_REMOVE_DIR); } +static int hook_path_truncate(const struct path *const path) +{ + return current_check_access_path(path, LANDLOCK_ACCESS_FS_TRUNCATE); +} + /* File hooks */ static inline access_mask_t get_file_access(const struct file *const file) @@ -1159,22 +1206,55 @@ static inline access_mask_t get_file_access(const struct file *const file) /* __FMODE_EXEC is indeed part of f_flags, not f_mode. */ if (file->f_flags & __FMODE_EXEC) access |= LANDLOCK_ACCESS_FS_EXECUTE; + return access; } static int hook_file_open(struct file *const file) { + access_mask_t access_req, access_rights; + const access_mask_t optional_rights = LANDLOCK_ACCESS_FS_TRUNCATE; const struct landlock_ruleset *const dom = landlock_get_current_domain(); - if (!dom) + if (!dom) { + /* Grant all rights. */ + landlock_file(file)->rights = LANDLOCK_MASK_ACCESS_FS; return 0; + } + /* * Because a file may be opened with O_PATH, get_file_access() may * return 0. This case will be handled with a future Landlock * evolution. */ - return check_access_path(dom, &file->f_path, get_file_access(file)); + access_req = get_file_access(file); + access_rights = get_path_access_rights(dom, &file->f_path, + access_req | optional_rights); + if (access_req & ~access_rights) + return -EACCES; + + /* + * For operations on already opened files (i.e. ftruncate()), it is the + * access rights at the time of open() which decide whether the + * operation is permitted. Therefore, we record the relevant subset of + * file access rights in the opened struct file. + */ + landlock_file(file)->rights = access_rights; + + return 0; +} + +static int hook_file_truncate(struct file *const file) +{ + /* + * We permit truncation if the truncation right was available at the + * time of opening the file. + */ + if (!(landlock_file(file)->rights & LANDLOCK_ACCESS_FS_TRUNCATE)) + return -EACCES; + + return 0; } static struct security_hook_list landlock_hooks[] __lsm_ro_after_init = { @@ -1194,6 +1274,8 @@ static struct security_hook_list landlock_hooks[] __lsm_ro_after_init = { LSM_HOOK_INIT(path_symlink, hook_path_symlink), LSM_HOOK_INIT(path_unlink, hook_path_unlink), LSM_HOOK_INIT(path_rmdir, hook_path_rmdir), + LSM_HOOK_INIT(path_truncate, hook_path_truncate), + LSM_HOOK_INIT(file_truncate, hook_file_truncate), LSM_HOOK_INIT(file_open, hook_file_open), }; diff --git a/security/landlock/fs.h b/security/landlock/fs.h index 8db7acf9109b..275ba5375839 100644 --- a/security/landlock/fs.h +++ b/security/landlock/fs.h @@ -36,6 +36,18 @@ struct landlock_inode_security { struct landlock_object __rcu *object; }; +/** + * struct landlock_file_security - File security blob + * + * This information is populated when opening a file in hook_file_open, and + * tracks the relevant Landlock access rights that were available at the time + * of opening the file. Other LSM hooks use these rights in order to authorize + * operations on already opened files. + */ +struct landlock_file_security { + access_mask_t rights; +}; + /** * struct landlock_superblock_security - Superblock security blob * @@ -50,6 +62,12 @@ struct landlock_superblock_security { atomic_long_t inode_refs; }; +static inline struct landlock_file_security * +landlock_file(const struct file *const file) +{ + return file->f_security + landlock_blob_sizes.lbs_file; +} + static inline struct landlock_inode_security * landlock_inode(const struct inode *const inode) { diff --git a/security/landlock/limits.h b/security/landlock/limits.h index b54184ab9439..82288f0e9e5e 100644 --- a/security/landlock/limits.h +++ b/security/landlock/limits.h @@ -18,7 +18,7 @@ #define LANDLOCK_MAX_NUM_LAYERS 16 #define LANDLOCK_MAX_NUM_RULES U32_MAX -#define LANDLOCK_LAST_ACCESS_FS LANDLOCK_ACCESS_FS_REFER +#define LANDLOCK_LAST_ACCESS_FS LANDLOCK_ACCESS_FS_TRUNCATE #define LANDLOCK_MASK_ACCESS_FS ((LANDLOCK_LAST_ACCESS_FS << 1) - 1) #define LANDLOCK_NUM_ACCESS_FS __const_hweight64(LANDLOCK_MASK_ACCESS_FS) diff --git a/security/landlock/setup.c b/security/landlock/setup.c index f8e8e980454c..3f196d2ce4f9 100644 --- a/security/landlock/setup.c +++ b/security/landlock/setup.c @@ -19,6 +19,7 @@ bool landlock_initialized __lsm_ro_after_init = false; struct lsm_blob_sizes landlock_blob_sizes __lsm_ro_after_init = { .lbs_cred = sizeof(struct landlock_cred_security), + .lbs_file = sizeof(struct landlock_file_security), .lbs_inode = sizeof(struct landlock_inode_security), .lbs_superblock = sizeof(struct landlock_superblock_security), }; diff --git a/security/landlock/syscalls.c b/security/landlock/syscalls.c index 735a0865ea11..f4d6fc7ed17f 100644 --- a/security/landlock/syscalls.c +++ b/security/landlock/syscalls.c @@ -129,7 +129,7 @@ static const struct file_operations ruleset_fops = { .write = fop_dummy_write, }; -#define LANDLOCK_ABI_VERSION 2 +#define LANDLOCK_ABI_VERSION 3 /** * sys_landlock_create_ruleset - Create a new ruleset diff --git a/tools/testing/selftests/landlock/base_test.c b/tools/testing/selftests/landlock/base_test.c index da9290817866..72cdae277b02 100644 --- a/tools/testing/selftests/landlock/base_test.c +++ b/tools/testing/selftests/landlock/base_test.c @@ -75,7 +75,7 @@ TEST(abi_version) const struct landlock_ruleset_attr ruleset_attr = { .handled_access_fs = LANDLOCK_ACCESS_FS_READ_FILE, }; - ASSERT_EQ(2, landlock_create_ruleset(NULL, 0, + ASSERT_EQ(3, landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION)); ASSERT_EQ(-1, landlock_create_ruleset(&ruleset_attr, 0, diff --git a/tools/testing/selftests/landlock/fs_test.c b/tools/testing/selftests/landlock/fs_test.c index 45de42a027c5..87b28d14a1aa 100644 --- a/tools/testing/selftests/landlock/fs_test.c +++ b/tools/testing/selftests/landlock/fs_test.c @@ -406,9 +406,10 @@ TEST_F_FORK(layout1, inval) #define ACCESS_FILE ( \ LANDLOCK_ACCESS_FS_EXECUTE | \ LANDLOCK_ACCESS_FS_WRITE_FILE | \ - LANDLOCK_ACCESS_FS_READ_FILE) + LANDLOCK_ACCESS_FS_READ_FILE | \ + LANDLOCK_ACCESS_FS_TRUNCATE) -#define ACCESS_LAST LANDLOCK_ACCESS_FS_REFER +#define ACCESS_LAST LANDLOCK_ACCESS_FS_TRUNCATE #define ACCESS_ALL ( \ ACCESS_FILE | \ @@ -422,7 +423,7 @@ TEST_F_FORK(layout1, inval) LANDLOCK_ACCESS_FS_MAKE_FIFO | \ LANDLOCK_ACCESS_FS_MAKE_BLOCK | \ LANDLOCK_ACCESS_FS_MAKE_SYM | \ - ACCESS_LAST) + LANDLOCK_ACCESS_FS_REFER) /* clang-format on */ From patchwork Thu Sep 8 19:58:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?G=C3=BCnther_Noack?= X-Patchwork-Id: 12970628 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 83EA8C54EE9 for ; Thu, 8 Sep 2022 19:59:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229824AbiIHT7c (ORCPT ); Thu, 8 Sep 2022 15:59:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44726 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229961AbiIHT6w (ORCPT ); Thu, 8 Sep 2022 15:58:52 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75602113C73; Thu, 8 Sep 2022 12:58:18 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id t7so22977339wrm.10; Thu, 08 Sep 2022 12:58:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=1TgRor+i7jhjtjXRxYgJ6mSQTAX5iEuASum3OKZiZ7s=; b=Tx7QalQ2VPw3yM/BAdW2S38Cgh5o3VbEC4UULY+qh4ycDEilaOg1UVPr3dteMtu8jg Up8JdhEB9lDlMwwJXjtnA/V0xrnLbquR3FbeugumYKxQLf/8+f2aYcslSp/Gji2IaLle l+J5yWn7ludtsV253ILH0ysOB0g2z4dO6jOjMhx67pzkTRKz++74Bi1n1l42wMJ0o8lm aTqSFJIhRmGiI4o9Ik+ZAnwwTpACt+mk62qsb89Cl2vZ7ga34yBGueMQVFVITWEAUFzM 7Oeu/7ZxKWwpce3PFK1vRyoHx6GxvME8ge5MCgE34VEnpi4SDoaOiZzvFma3p+LChONl hQpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=1TgRor+i7jhjtjXRxYgJ6mSQTAX5iEuASum3OKZiZ7s=; b=wmjECEe6Vf+LhkuwUWVa3OH3+rYksN8+7mK+/I6XnnE0qOZAnAJh/vZhWBpY0W2Kwx VXdquM5A87Z6pPhZ5eCcnc12xNb2pnNQV7hZ1fnzu9BXxqGHgipN91J6qQrv2+ecXtWc MEooXHz9a9Lm+DMV8C1+u3pJ3XAzvWAcw3qU9cVX/hdxyIjOarC1tUmYyvTgX6fMjm47 hHnXqJHrSRC+A5w0xTE62Mixn388geLH8H4YfPl1IoeM61AY1h6uwPTd3LUcRyEFvdda ef+fSpUQg1l/sFnmhznEAnU4iZL5SEe9JrxoEUIOu0HzE9bqLfs78Xz/U+TBodiDuL3g rUUw== X-Gm-Message-State: ACgBeo0o1jrVRzcwfMflPF0rmKeoSMu9eYI+MepW2ulPptTZ5DtPMlVI 0SvVOtEmr+v+ayd+UZ9qZAhm/pobk70= X-Google-Smtp-Source: AA6agR44ouxRr+1E8kYkLGVK+Aaqxb9qSOeC3tgNRo8zF+IMiuU4PCUxNZFhBmsRsGyMdZpx+ErIiQ== X-Received: by 2002:a05:6000:1c0d:b0:225:6c66:5ed3 with SMTP id ba13-20020a0560001c0d00b002256c665ed3mr6224299wrb.678.1662667093224; Thu, 08 Sep 2022 12:58:13 -0700 (PDT) Received: from nuc.i.gnoack.org ([2a02:168:633b:1:1e69:7aff:fe05:97e6]) by smtp.gmail.com with ESMTPSA id a22-20020a05600c2d5600b003a541d893desm3360682wmg.38.2022.09.08.12.58.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Sep 2022 12:58:12 -0700 (PDT) From: =?utf-8?q?G=C3=BCnther_Noack?= To: linux-security-module@vger.kernel.org Cc: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= , James Morris , Paul Moore , "Serge E . Hallyn" , linux-fsdevel@vger.kernel.org, Konstantin Meskhidze , =?utf-8?q?G=C3=BCnt?= =?utf-8?q?her_Noack?= Subject: [PATCH v6 3/5] selftests/landlock: Selftests for file truncation support Date: Thu, 8 Sep 2022 21:58:03 +0200 Message-Id: <20220908195805.128252-4-gnoack3000@gmail.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220908195805.128252-1-gnoack3000@gmail.com> References: <20220908195805.128252-1-gnoack3000@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: These tests exercise the following truncation operations: * truncate() (truncate by path) * ftruncate() (truncate by file descriptor) * open with the O_TRUNC flag * special case: creat(), which is open with O_CREAT|O_WRONLY|O_TRUNC. in the following scenarios: * Files with read, write and truncate rights. * Files with read and truncate rights. * Files with the truncate right. * Files without the truncate right. In particular, the following scenarios are enforced with the test: * open() with O_TRUNC requires the truncate right, if it truncates a file. open() already checks security_path_truncate() in this case, and it required no additional check in the Landlock LSM's file_open hook. * creat() requires the truncate right when called with an existing filename. * creat() does *not* require the truncate right when it's creating a new file. * ftruncate() requires that the file was opened by a thread that had the truncate right for the file at the time of open(). (The rights are carried along with the opened file.) Signed-off-by: Günther Noack --- tools/testing/selftests/landlock/fs_test.c | 280 +++++++++++++++++++++ 1 file changed, 280 insertions(+) diff --git a/tools/testing/selftests/landlock/fs_test.c b/tools/testing/selftests/landlock/fs_test.c index 87b28d14a1aa..ddc8c7e57e86 100644 --- a/tools/testing/selftests/landlock/fs_test.c +++ b/tools/testing/selftests/landlock/fs_test.c @@ -58,6 +58,7 @@ static const char file1_s2d3[] = TMP_DIR "/s2d1/s2d2/s2d3/f1"; static const char file2_s2d3[] = TMP_DIR "/s2d1/s2d2/s2d3/f2"; static const char dir_s3d1[] = TMP_DIR "/s3d1"; +static const char file1_s3d1[] = TMP_DIR "/s3d1/f1"; /* dir_s3d2 is a mount point. */ static const char dir_s3d2[] = TMP_DIR "/s3d1/s3d2"; static const char dir_s3d3[] = TMP_DIR "/s3d1/s3d2/s3d3"; @@ -83,6 +84,7 @@ static const char dir_s3d3[] = TMP_DIR "/s3d1/s3d2/s3d3"; * │   ├── f1 * │   └── f2 * └── s3d1 + *    ├── f1 * └── s3d2 * └── s3d3 */ @@ -208,6 +210,7 @@ static void create_layout1(struct __test_metadata *const _metadata) create_file(_metadata, file1_s2d3); create_file(_metadata, file2_s2d3); + create_file(_metadata, file1_s3d1); create_directory(_metadata, dir_s3d2); set_cap(_metadata, CAP_SYS_ADMIN); ASSERT_EQ(0, mount("tmp", dir_s3d2, "tmpfs", 0, "size=4m,mode=700")); @@ -230,6 +233,7 @@ static void remove_layout1(struct __test_metadata *const _metadata) EXPECT_EQ(0, remove_path(file1_s2d2)); EXPECT_EQ(0, remove_path(file1_s2d1)); + EXPECT_EQ(0, remove_path(file1_s3d1)); EXPECT_EQ(0, remove_path(dir_s3d3)); set_cap(_metadata, CAP_SYS_ADMIN); umount(dir_s3d2); @@ -3158,6 +3162,282 @@ TEST_F_FORK(layout1, proc_pipe) ASSERT_EQ(0, close(pipe_fds[1])); } +/* Invokes truncate(2) and returns its errno or 0. */ +static int test_truncate(const char *const path) +{ + if (truncate(path, 10) < 0) + return errno; + return 0; +} + +/* + * Invokes creat(2) and returns its errno or 0. + * Closes the opened file descriptor on success. + */ +static int test_creat(const char *const path) +{ + int fd = creat(path, 0600); + + if (fd < 0) + return errno; + + /* + * Mixing error codes from close(2) and creat(2) should not lead to any + * (access type) confusion for this test. + */ + if (close(fd) < 0) + return errno; + return 0; +} + +/* + * Exercises file truncation when it's not restricted, + * as it was the case before LANDLOCK_ACCESS_FS_TRUNCATE existed. + */ +TEST_F_FORK(layout1, truncate_unhandled) +{ + const char *const file_r = file1_s1d1; + const char *const file_w = file2_s1d1; + const char *const file_none = file1_s1d2; + const struct rule rules[] = { + { + .path = file_r, + .access = LANDLOCK_ACCESS_FS_READ_FILE, + }, + { + .path = file_w, + .access = LANDLOCK_ACCESS_FS_WRITE_FILE, + }, + /* Implicitly: No rights for file_none. */ + {}, + }; + + const __u64 handled = LANDLOCK_ACCESS_FS_READ_FILE | + LANDLOCK_ACCESS_FS_WRITE_FILE; + int ruleset_fd; + + /* Enable Landlock. */ + ruleset_fd = create_ruleset(_metadata, handled, rules); + + ASSERT_LE(0, ruleset_fd); + enforce_ruleset(_metadata, ruleset_fd); + ASSERT_EQ(0, close(ruleset_fd)); + + /* + * Checks read right: truncate and open with O_TRUNC work, unless the + * file is attempted to be opened for writing. + */ + EXPECT_EQ(0, test_truncate(file_r)); + EXPECT_EQ(0, test_open(file_r, O_RDONLY | O_TRUNC)); + EXPECT_EQ(EACCES, test_open(file_r, O_WRONLY | O_TRUNC)); + EXPECT_EQ(EACCES, test_creat(file_r)); + + /* + * Checks write right: truncate and open with O_TRUNC work, unless the + * file is attempted to be opened for reading. + */ + EXPECT_EQ(0, test_truncate(file_w)); + EXPECT_EQ(EACCES, test_open(file_w, O_RDONLY | O_TRUNC)); + EXPECT_EQ(0, test_open(file_w, O_WRONLY | O_TRUNC)); + EXPECT_EQ(0, test_creat(file_w)); + + /* + * Checks "no rights" case: truncate works but all open attempts fail, + * including creat. + */ + EXPECT_EQ(0, test_truncate(file_none)); + EXPECT_EQ(EACCES, test_open(file_none, O_RDONLY | O_TRUNC)); + EXPECT_EQ(EACCES, test_open(file_none, O_WRONLY | O_TRUNC)); + EXPECT_EQ(EACCES, test_creat(file_none)); +} + +TEST_F_FORK(layout1, truncate) +{ + const char *const file_rwt = file1_s1d1; + const char *const file_rw = file2_s1d1; + const char *const file_rt = file1_s1d2; + const char *const file_t = file2_s1d2; + const char *const file_none = file1_s1d3; + const char *const dir_t = dir_s2d1; + const char *const file_in_dir_t = file1_s2d1; + const char *const dir_w = dir_s3d1; + const char *const file_in_dir_w = file1_s3d1; + int file_rwt_fd, file_rw_fd; + const struct rule rules[] = { + { + .path = file_rwt, + .access = LANDLOCK_ACCESS_FS_READ_FILE | + LANDLOCK_ACCESS_FS_WRITE_FILE | + LANDLOCK_ACCESS_FS_TRUNCATE, + }, + { + .path = file_rw, + .access = LANDLOCK_ACCESS_FS_READ_FILE | + LANDLOCK_ACCESS_FS_WRITE_FILE, + }, + { + .path = file_rt, + .access = LANDLOCK_ACCESS_FS_READ_FILE | + LANDLOCK_ACCESS_FS_TRUNCATE, + }, + { + .path = file_t, + .access = LANDLOCK_ACCESS_FS_TRUNCATE, + }, + /* Implicitly: No access rights for file_none. */ + { + .path = dir_t, + .access = LANDLOCK_ACCESS_FS_TRUNCATE, + }, + { + .path = dir_w, + .access = LANDLOCK_ACCESS_FS_WRITE_FILE, + }, + {}, + }; + const __u64 handled = LANDLOCK_ACCESS_FS_READ_FILE | + LANDLOCK_ACCESS_FS_WRITE_FILE | + LANDLOCK_ACCESS_FS_TRUNCATE; + int ruleset_fd; + + /* Enable Landlock. */ + ruleset_fd = create_ruleset(_metadata, handled, rules); + + ASSERT_LE(0, ruleset_fd); + enforce_ruleset(_metadata, ruleset_fd); + ASSERT_EQ(0, close(ruleset_fd)); + + /* Checks read, write and truncate rights: truncation works. */ + EXPECT_EQ(0, test_truncate(file_rwt)); + EXPECT_EQ(0, test_open(file_rwt, O_RDONLY | O_TRUNC)); + EXPECT_EQ(0, test_open(file_rwt, O_WRONLY | O_TRUNC)); + + /* Checks read and write rights: no truncate variant works. */ + EXPECT_EQ(EACCES, test_truncate(file_rw)); + EXPECT_EQ(EACCES, test_open(file_rw, O_RDONLY | O_TRUNC)); + EXPECT_EQ(EACCES, test_open(file_rw, O_WRONLY | O_TRUNC)); + + /* + * Checks read and truncate rights: truncation works. + * + * Note: Files can get truncated using open() even with O_RDONLY. + */ + EXPECT_EQ(0, test_truncate(file_rt)); + EXPECT_EQ(0, test_open(file_rt, O_RDONLY | O_TRUNC)); + EXPECT_EQ(EACCES, test_open(file_rt, O_WRONLY | O_TRUNC)); + + /* Checks truncate right: truncate works, but can't open file. */ + EXPECT_EQ(0, test_truncate(file_t)); + EXPECT_EQ(EACCES, test_open(file_t, O_RDONLY | O_TRUNC)); + EXPECT_EQ(EACCES, test_open(file_t, O_WRONLY | O_TRUNC)); + + /* Checks "no rights" case: No form of truncation works. */ + EXPECT_EQ(EACCES, test_truncate(file_none)); + EXPECT_EQ(EACCES, test_open(file_none, O_RDONLY | O_TRUNC)); + EXPECT_EQ(EACCES, test_open(file_none, O_WRONLY | O_TRUNC)); + + /* + * Checks truncate right on directory: truncate works on contained + * files. + */ + EXPECT_EQ(0, test_truncate(file_in_dir_t)); + EXPECT_EQ(EACCES, test_open(file_in_dir_t, O_RDONLY | O_TRUNC)); + EXPECT_EQ(EACCES, test_open(file_in_dir_t, O_WRONLY | O_TRUNC)); + + /* + * Checks creat in dir_w: This requires the truncate right when + * overwriting an existing file, but does not require it when the file + * is new. + */ + EXPECT_EQ(EACCES, test_creat(file_in_dir_w)); + + ASSERT_EQ(0, unlink(file_in_dir_w)); + EXPECT_EQ(0, test_creat(file_in_dir_w)); +} + +static void landlock_single_path(struct __test_metadata *const _metadata, + const char *const path, __u64 handled, + __u64 permitted) +{ + const struct rule rules[] = { + { + .path = path, + .access = permitted, + }, + {}, + }; + int ruleset_fd = create_ruleset(_metadata, handled, rules); + + ASSERT_LE(0, ruleset_fd); + + enforce_ruleset(_metadata, ruleset_fd); + + ASSERT_EQ(0, close(ruleset_fd)); +} + +/* Invokes ftruncate(2) and returns its errno or 0. */ +static int test_ftruncate(int fd) +{ + if (ftruncate(fd, 10) < 0) + return errno; + return 0; +} + +TEST_F_FORK(layout1, ftruncate) +{ + /* + * This test opens a new file descriptor at different stages of + * Landlock restriction: + * + * without restriction: ftruncate works + * something else but truncate restricted: ftruncate works + * truncate restricted and permitted: ftruncate works + * truncate restricted and not permitted: ftruncate fails + * + * Whether this works or not is expected to depend on the time when the + * FD was opened, not to depend on the time when ftruncate() was + * called. + */ + const char *const path = file1_s1d1; + int fd0, fd1, fd2, fd3; + + fd0 = open(path, O_WRONLY); + EXPECT_EQ(0, test_ftruncate(fd0)); + + landlock_single_path(_metadata, path, + LANDLOCK_ACCESS_FS_READ_FILE | + LANDLOCK_ACCESS_FS_WRITE_FILE, + LANDLOCK_ACCESS_FS_WRITE_FILE); + + fd1 = open(path, O_WRONLY); + EXPECT_EQ(0, test_ftruncate(fd0)); + EXPECT_EQ(0, test_ftruncate(fd1)); + + landlock_single_path(_metadata, path, LANDLOCK_ACCESS_FS_TRUNCATE, + LANDLOCK_ACCESS_FS_TRUNCATE); + + fd2 = open(path, O_WRONLY); + EXPECT_EQ(0, test_ftruncate(fd0)); + EXPECT_EQ(0, test_ftruncate(fd1)); + EXPECT_EQ(0, test_ftruncate(fd2)); + + landlock_single_path(_metadata, path, + LANDLOCK_ACCESS_FS_TRUNCATE | + LANDLOCK_ACCESS_FS_WRITE_FILE, + LANDLOCK_ACCESS_FS_WRITE_FILE); + + fd3 = open(path, O_WRONLY); + EXPECT_EQ(0, test_ftruncate(fd0)); + EXPECT_EQ(0, test_ftruncate(fd1)); + EXPECT_EQ(0, test_ftruncate(fd2)); + EXPECT_EQ(EACCES, test_ftruncate(fd3)); + + ASSERT_EQ(0, close(fd0)); + ASSERT_EQ(0, close(fd1)); + ASSERT_EQ(0, close(fd2)); + ASSERT_EQ(0, close(fd3)); +} + /* clang-format off */ FIXTURE(layout1_bind) {}; /* clang-format on */ From patchwork Thu Sep 8 19:58:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?G=C3=BCnther_Noack?= X-Patchwork-Id: 12970630 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 02982C6FA86 for ; Thu, 8 Sep 2022 19:59:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229926AbiIHT7u (ORCPT ); Thu, 8 Sep 2022 15:59:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44430 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230001AbiIHT65 (ORCPT ); Thu, 8 Sep 2022 15:58:57 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D64A8114A45; Thu, 8 Sep 2022 12:58:18 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id b17so14642883wrq.3; Thu, 08 Sep 2022 12:58:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=98FGazg/gj0QUvoyFacHOBPUOa+vPrLf+ppcBewd70Y=; b=agSYvr++xJOiDpjl0/QYv419PaTU4e8lsz6BSiq2FbIw+NGXc/aXm5EjmOEFzWn2EX XRfl+LeipulpMrRoJjTtU7Y7KxlHcEikOjc5dvIAWdpyF1GUk0XiYvwvJVdPipvCgxJo VO83f3pzy4Dl3c27t2JLwbyi9kTi2tk/WwqaFDBCwUWDXywyMQqaW2Bu36gQhekylFLA ZKLHkrguwTbKWpxy77idC4uo0KRQ0kwb/DiQ7hZofTy/Hpz5MvXGQhTyDZ78D2cNz6jX +uy8aKsoGqwmQxzpuZEE4NyQBcyFKDRWcHRr/wSDBaQp0/rf9R2vpqzdqm5EZB7m0+E/ RIkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=98FGazg/gj0QUvoyFacHOBPUOa+vPrLf+ppcBewd70Y=; b=tn+x4e8FE7ntuYnmkxKgoSOwn9UEn3q8IMleVmD5OnpZosbLu8SF8PWxjDDf9FNZiJ 0WhlDRXA705xvsXzcLbZFvdz9wKRvuY1mLdV2rIxM3fwsV1Ce8L/4fKXOpijrdd3pHAu Ao9i1b0rxkLyk69PMlZYdPviaLxqWCkOqSuKG2+WbtxVwc25brSqiUvYxDhzfc61SYb+ oPS2oAlnXytF+OVhe4tfzqMF48MqNXOyAekgsq8OIlsAJh8VhphLEYVd12NecKDVcBzj JNzqnmciLWY+OQ8Tx3aeUC/w4MEsLAM++SzoU3ZS+vo+tRqkcj+z6xMzHx0NJM/e/6AA zJbQ== X-Gm-Message-State: ACgBeo0Rm0q7XmHs+jcFH5Zt9dYpHhI8kNUt5eNmZOW3IC+bT6dFaATP u92WSFFLrFAQ83fStqI5aPA+QXLQAO4= X-Google-Smtp-Source: AA6agR6Xek2Lo64utNaRTTAMQV4Y4Zegpo+snrGEbR8RXCIOodiTx3y8Q1ija8OWoUWcU1iCiODNJg== X-Received: by 2002:a5d:6f14:0:b0:228:e3c6:fa2 with SMTP id ay20-20020a5d6f14000000b00228e3c60fa2mr5652663wrb.516.1662667094140; Thu, 08 Sep 2022 12:58:14 -0700 (PDT) Received: from nuc.i.gnoack.org ([2a02:168:633b:1:1e69:7aff:fe05:97e6]) by smtp.gmail.com with ESMTPSA id a22-20020a05600c2d5600b003a541d893desm3360682wmg.38.2022.09.08.12.58.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Sep 2022 12:58:13 -0700 (PDT) From: =?utf-8?q?G=C3=BCnther_Noack?= To: linux-security-module@vger.kernel.org Cc: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= , James Morris , Paul Moore , "Serge E . Hallyn" , linux-fsdevel@vger.kernel.org, Konstantin Meskhidze , =?utf-8?q?G=C3=BCnt?= =?utf-8?q?her_Noack?= Subject: [PATCH v6 4/5] samples/landlock: Extend sample tool to support LANDLOCK_ACCESS_FS_TRUNCATE Date: Thu, 8 Sep 2022 21:58:04 +0200 Message-Id: <20220908195805.128252-5-gnoack3000@gmail.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220908195805.128252-1-gnoack3000@gmail.com> References: <20220908195805.128252-1-gnoack3000@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: Update the sandboxer sample to restrict truncate actions. This is automatically enabled by default if the running kernel supports LANDLOCK_ACCESS_FS_TRUNCATE, expect for the paths listed in the LL_FS_RW environment variable. Signed-off-by: Günther Noack --- samples/landlock/sandboxer.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/samples/landlock/sandboxer.c b/samples/landlock/sandboxer.c index 3e404e51ec64..771b6b10d519 100644 --- a/samples/landlock/sandboxer.c +++ b/samples/landlock/sandboxer.c @@ -76,7 +76,8 @@ static int parse_path(char *env_path, const char ***const path_list) #define ACCESS_FILE ( \ LANDLOCK_ACCESS_FS_EXECUTE | \ LANDLOCK_ACCESS_FS_WRITE_FILE | \ - LANDLOCK_ACCESS_FS_READ_FILE) + LANDLOCK_ACCESS_FS_READ_FILE | \ + LANDLOCK_ACCESS_FS_TRUNCATE) /* clang-format on */ @@ -160,10 +161,8 @@ static int populate_ruleset(const char *const env_var, const int ruleset_fd, LANDLOCK_ACCESS_FS_MAKE_FIFO | \ LANDLOCK_ACCESS_FS_MAKE_BLOCK | \ LANDLOCK_ACCESS_FS_MAKE_SYM | \ - LANDLOCK_ACCESS_FS_REFER) - -#define ACCESS_ABI_2 ( \ - LANDLOCK_ACCESS_FS_REFER) + LANDLOCK_ACCESS_FS_REFER | \ + LANDLOCK_ACCESS_FS_TRUNCATE) /* clang-format on */ @@ -226,11 +225,17 @@ int main(const int argc, char *const argv[], char *const *const envp) return 1; } /* Best-effort security. */ - if (abi < 2) { - ruleset_attr.handled_access_fs &= ~ACCESS_ABI_2; - access_fs_ro &= ~ACCESS_ABI_2; - access_fs_rw &= ~ACCESS_ABI_2; + switch (abi) { + case 1: + /* Removes LANDLOCK_ACCESS_FS_REFER for ABI < 2 */ + ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER; + __attribute__((fallthrough)); + case 2: + /* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */ + ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE; } + access_fs_ro &= ruleset_attr.handled_access_fs; + access_fs_rw &= ruleset_attr.handled_access_fs; ruleset_fd = landlock_create_ruleset(&ruleset_attr, sizeof(ruleset_attr), 0); From patchwork Thu Sep 8 19:58:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?G=C3=BCnther_Noack?= X-Patchwork-Id: 12970629 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 70672C54EE9 for ; Thu, 8 Sep 2022 19:59:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229909AbiIHT7s (ORCPT ); Thu, 8 Sep 2022 15:59:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229994AbiIHT64 (ORCPT ); Thu, 8 Sep 2022 15:58:56 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E88C1114A46; Thu, 8 Sep 2022 12:58:18 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id bq9so15076378wrb.4; Thu, 08 Sep 2022 12:58:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=QRKJty4JdUmvaojGTlGrAJV1Z6zD33ZGAy1IL0bKdMs=; b=KqfnRH5VosRmystPbhSAtj3K7cPU3jJ3nRVWtglrC5Ta6W/gmdfIcjpp5lgBCDfGbt PU2wacbSrFX7l8FmrPNAApVzbUHXBKgug8jfPmTnbHwisOWwtqGz8FgVK8qgN/0TUmA/ xkWF8b45LDY4ckvdv1W4RgWqR1QgOy7YiIs8zDY910CsTwBE9kbINEijgJO6gA11Prjo jCoOqg5RekhMY3lH+BNBmuE9CGdT106yC5aMIYNlkuMeDkNBPfjibwEBfXqfzS9IspWU NECLu/AUQEGKO6IQunaPKGjZgBSJvBRUy9TNVuhaZXzv0bnoaZxNlGtGPi5Mf+FlhdEb YK8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=QRKJty4JdUmvaojGTlGrAJV1Z6zD33ZGAy1IL0bKdMs=; b=PymZloFvtPz/Up6424LYRAnO2LaPZF3Tylt/aGROPBgBkt6Bdwd6YeJNaE7rwOcqgB FZEwy+LpaQi/iNLEgd+cScTw6nM+xpasCpR8LGiCR3of0qBhordgpeI7gaW9GvZXQpA2 22OvLwZjSc+Wy+2kX2Lj0DWVQ9Sm+B39rjo97CzdkdVKiEABP0zQ6zktkLxYEvHKxbcg FjYnQ1omvvHens8wbfBqk3XTuPuIdC2UgodK8gpUT0FJ3AFOytNf5ehhJA0PC5DhQRYi z6/AR+2clHt77u2AkkHbZg6xRh1ngbJI/8DT2FJko6TAxWEbXq8zMMuIUzDgwcvRcBGU vAaQ== X-Gm-Message-State: ACgBeo0dlfkZnPB2skyA/kvCyz+swwq2npH0lVJxJWmB8IRw1Uc1td9J 13J4Kq3+WAt7mvCn1P5laQFouNm4fuA= X-Google-Smtp-Source: AA6agR6tuunq4wfCM8i9Q2HWeE3h5hTtepy8+aBJKIZ1qsf1At0cxfqmrQ8Bo+hTCpNWOvgJ6w8WWA== X-Received: by 2002:a05:6000:12c5:b0:228:d235:4daa with SMTP id l5-20020a05600012c500b00228d2354daamr6158841wrx.595.1662667095297; Thu, 08 Sep 2022 12:58:15 -0700 (PDT) Received: from nuc.i.gnoack.org ([2a02:168:633b:1:1e69:7aff:fe05:97e6]) by smtp.gmail.com with ESMTPSA id a22-20020a05600c2d5600b003a541d893desm3360682wmg.38.2022.09.08.12.58.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Sep 2022 12:58:14 -0700 (PDT) From: =?utf-8?q?G=C3=BCnther_Noack?= To: linux-security-module@vger.kernel.org Cc: =?utf-8?q?Micka=C3=ABl_Sala=C3=BCn?= , James Morris , Paul Moore , "Serge E . Hallyn" , linux-fsdevel@vger.kernel.org, Konstantin Meskhidze , =?utf-8?q?G=C3=BCnt?= =?utf-8?q?her_Noack?= Subject: [PATCH v6 5/5] landlock: Document Landlock's file truncation support Date: Thu, 8 Sep 2022 21:58:05 +0200 Message-Id: <20220908195805.128252-6-gnoack3000@gmail.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220908195805.128252-1-gnoack3000@gmail.com> References: <20220908195805.128252-1-gnoack3000@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: Use the LANDLOCK_ACCESS_FS_TRUNCATE flag in the tutorial. Adapt the backwards compatibility example and discussion to remove the truncation flag where needed. Point out potential surprising behaviour related to truncate. Signed-off-by: Günther Noack --- Documentation/userspace-api/landlock.rst | 62 +++++++++++++++++++++--- 1 file changed, 54 insertions(+), 8 deletions(-) diff --git a/Documentation/userspace-api/landlock.rst b/Documentation/userspace-api/landlock.rst index b8ea59493964..57802fd1e09b 100644 --- a/Documentation/userspace-api/landlock.rst +++ b/Documentation/userspace-api/landlock.rst @@ -8,7 +8,7 @@ Landlock: unprivileged access control ===================================== :Author: Mickaël Salaün -:Date: May 2022 +:Date: September 2022 The goal of Landlock is to enable to restrict ambient rights (e.g. global filesystem access) for a set of processes. Because Landlock is a stackable @@ -60,7 +60,8 @@ the need to be explicit about the denied-by-default access rights. LANDLOCK_ACCESS_FS_MAKE_FIFO | LANDLOCK_ACCESS_FS_MAKE_BLOCK | LANDLOCK_ACCESS_FS_MAKE_SYM | - LANDLOCK_ACCESS_FS_REFER, + LANDLOCK_ACCESS_FS_REFER | + LANDLOCK_ACCESS_FS_TRUNCATE, }; Because we may not know on which kernel version an application will be @@ -69,16 +70,26 @@ should try to protect users as much as possible whatever the kernel they are using. To avoid binary enforcement (i.e. either all security features or none), we can leverage a dedicated Landlock command to get the current version of the Landlock ABI and adapt the handled accesses. Let's check if we should -remove the `LANDLOCK_ACCESS_FS_REFER` access right which is only supported -starting with the second version of the ABI. +remove the `LANDLOCK_ACCESS_FS_REFER` or `LANDLOCK_ACCESS_FS_TRUNCATE` access +rights, which are only supported starting with the second and third version of +the ABI. .. code-block:: c int abi; abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION); - if (abi < 2) { - ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER; + switch (abi) { + case -1: + perror("The running kernel does not enable to use Landlock"); + return 1; + case 1: + /* Removes LANDLOCK_ACCESS_FS_REFER for ABI < 2 */ + ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_REFER; + __attribute__((fallthrough)); + case 2: + /* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */ + ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE; } This enables to create an inclusive ruleset that will contain our rules. @@ -127,8 +138,8 @@ descriptor. It may also be required to create rules following the same logic as explained for the ruleset creation, by filtering access rights according to the Landlock -ABI version. In this example, this is not required because -`LANDLOCK_ACCESS_FS_REFER` is not allowed by any rule. +ABI version. In this example, this is not required because all of the requested +``allowed_access`` rights are already available in ABI 1. We now have a ruleset with one rule allowing read access to ``/usr`` while denying all other handled accesses for the filesystem. The next step is to @@ -251,6 +262,32 @@ To be allowed to use :manpage:`ptrace(2)` and related syscalls on a target process, a sandboxed process should have a subset of the target process rules, which means the tracee must be in a sub-domain of the tracer. +Truncating files +---------------- + +The operations covered by `LANDLOCK_ACCESS_FS_WRITE_FILE` and +`LANDLOCK_ACCESS_FS_TRUNCATE` both change the contents of a file and sometimes +overlap in non-intuitive ways. It is recommended to always specify both of +these together. + +A particularly surprising example is :manpage:`creat(2)`. The name suggests +that this system call requires the rights to create and write files. However, +it also requires the truncate right if an existing file under the same name is +already present. + +It should also be noted that truncating files does not require the +`LANDLOCK_ACCESS_FS_WRITE_FILE` right. Apart from the :manpage:`truncate(2)` +system call, this can also be done through :manpage:`open(2)` with the flags +`O_RDONLY | O_TRUNC`. + +When opening a file, the availability of the `LANDLOCK_ACCESS_FS_TRUNCATE` right +is associated with the newly created file descriptor and will be used for +subsequent truncation attempts using :manpage:`ftruncate(2)`. It is possible to +have multiple open file descriptors for the same file, where one grants the +right to truncate the file and the other does not. It is also possible to pass +such file descriptors between processes, keeping their Landlock properties, even +when these processes don't have an enforced Landlock ruleset. + Compatibility ============= @@ -397,6 +434,15 @@ Starting with the Landlock ABI version 2, it is now possible to securely control renaming and linking thanks to the new `LANDLOCK_ACCESS_FS_REFER` access right. +File truncation (ABI < 3) +------------------------- + +File truncation could not be denied before the third Landlock ABI, so it is +always allowed when using a kernel that only supports the first or second ABI. + +Starting with the Landlock ABI version 3, it is now possible to securely control +truncation thanks to the new `LANDLOCK_ACCESS_FS_TRUNCATE` access right. + .. _kernel_support: Kernel support