Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp2219317imm; Tue, 10 Jul 2018 15:44:38 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdFX5UQ68Z0hJhg6hcc74OFE8O95l+kjKJQ0ji6v1jeXi8NYEcSQpIcBV6b5SPhLAydGga5 X-Received: by 2002:a17:902:b587:: with SMTP id a7-v6mr26996860pls.225.1531262678445; Tue, 10 Jul 2018 15:44:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531262678; cv=none; d=google.com; s=arc-20160816; b=Q/wBgxXW3WEX/mN5x0iO1MQ1fa7KP6oTckf5e7uc0kcF3F39+XdmeZjmuXKs+Ud0Sf 11itQ3qUOgEKimqaGTusRWjQYcpJ+VW0PakBQYUVui/0K1Rj/on1yHuQJRemi6VZ/lIU fGM4r1euBogW0dfSqDxdMIpOR5wtcd97KKAHjhKhSLDHTGsAt+qIfM6M2UTJczbirOyB BkL+rkipqq3dVFfiX/07bIwwpsxW30mwDVxAeOu4YDJdiXC1k5qRigSlyVHPPuuLm3dM njj1Efn3YEg+xiaab0Yh9qxggLMVW6uiNagb8sQYkS6xkiZSoHNlYzVQnouxCA5EAU67 Igfg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:cc:to:from :subject:organization:arc-authentication-results; bh=KFpRP5ynRbPqig6pi6/eUgxOqQRQcwGKzb6xQzTRRLA=; b=mPULapxFIgllI4L8zfNXn4dgSeFOder2iACcfED9PBloW/0mKQdqNgsVecwz9qlRzc BqtPsLkvZVbbL07hYIPE0TNR51wTsgxS3DBkoLIdEX1n3VoxfLqvCvu0qTAlSu1H+hzS yrGpsZzPFwAZotX9lhSwiY0DteMNUEAjfZzthJ5eOuhAUZTyD948odaUmtNgRxtZF3bB yy/afpmx8d7/I52gHWGOhFuD8HUPBUXYzb16WNmYTwF59vbPEa1z+W3ps6pdd4M9Ua/d NTpQM2R7MbClJyh9jnZhWJcGQjTq28MjeYHCwIa+TeI9jRnfaxBB7lYWDK3PgmOeSN6C VDWA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q14-v6si17585458pgm.512.2018.07.10.15.44.23; Tue, 10 Jul 2018 15:44:38 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732589AbeGJWnq (ORCPT + 99 others); Tue, 10 Jul 2018 18:43:46 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:51608 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1732253AbeGJWnq (ORCPT ); Tue, 10 Jul 2018 18:43:46 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E1B9D40363A8; Tue, 10 Jul 2018 22:42:31 +0000 (UTC) Received: from warthog.procyon.org.uk (ovpn-120-149.rdu2.redhat.com [10.10.120.149]) by smtp.corp.redhat.com (Postfix) with ESMTP id D59A02026D6B; Tue, 10 Jul 2018 22:42:30 +0000 (UTC) Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 Subject: [PATCH 09/32] apparmor: Implement security hooks for the new mount API [ver #9] From: David Howells To: viro@zeniv.linux.org.uk Cc: John Johansen , apparmor@lists.ubuntu.com, linux-kernel@vger.kernel.org, dhowells@redhat.com, linux-security-module@vger.kernel.org, linux-fsdevel@vger.kernel.org, torvalds@linux-foundation.org Date: Tue, 10 Jul 2018 23:42:30 +0100 Message-ID: <153126255030.14533.17831362996848029702.stgit@warthog.procyon.org.uk> In-Reply-To: <153126248868.14533.9751473662727327569.stgit@warthog.procyon.org.uk> References: <153126248868.14533.9751473662727327569.stgit@warthog.procyon.org.uk> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Tue, 10 Jul 2018 22:42:31 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Tue, 10 Jul 2018 22:42:31 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'dhowells@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Implement hooks to check the creation of new mountpoints for AppArmor. Unfortunately, the DFA evaluation puts the option data in last, after the details of the mountpoint, so we have to cache the mount options in the fs_context using those hooks till we get to the new mountpoint hook. Signed-off-by: David Howells Acked-by: John Johansen cc: apparmor@lists.ubuntu.com cc: linux-security-module@vger.kernel.org --- security/apparmor/include/mount.h | 11 +++++ security/apparmor/lsm.c | 80 +++++++++++++++++++++++++++++++++++++ security/apparmor/mount.c | 46 +++++++++++++++++++++ 3 files changed, 135 insertions(+), 2 deletions(-) diff --git a/security/apparmor/include/mount.h b/security/apparmor/include/mount.h index 25d6067fa6ef..0441bfae30fa 100644 --- a/security/apparmor/include/mount.h +++ b/security/apparmor/include/mount.h @@ -16,6 +16,7 @@ #include #include +#include #include "domain.h" #include "policy.h" @@ -27,7 +28,13 @@ #define AA_AUDIT_DATA 0x40 #define AA_MNT_CONT_MATCH 0x40 -#define AA_MS_IGNORE_MASK (MS_KERNMOUNT | MS_NOSEC | MS_ACTIVE | MS_BORN) +#define AA_SB_IGNORE_MASK (SB_KERNMOUNT | SB_NOSEC | SB_ACTIVE | SB_BORN) + +struct apparmor_fs_context { + struct fs_context fc; + char *saved_options; + size_t saved_size; +}; int aa_remount(struct aa_label *label, const struct path *path, unsigned long flags, void *data); @@ -45,6 +52,8 @@ int aa_move_mount(struct aa_label *label, const struct path *path, int aa_new_mount(struct aa_label *label, const char *dev_name, const struct path *path, const char *type, unsigned long flags, void *data); +int aa_new_mount_fc(struct aa_label *label, struct fs_context *fc, + const struct path *mountpoint); int aa_umount(struct aa_label *label, struct vfsmount *mnt, int flags); diff --git a/security/apparmor/lsm.c b/security/apparmor/lsm.c index c65307dcd652..29803dc604f8 100644 --- a/security/apparmor/lsm.c +++ b/security/apparmor/lsm.c @@ -520,6 +520,78 @@ static int apparmor_file_mprotect(struct vm_area_struct *vma, !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0); } +static int apparmor_fs_context_alloc(struct fs_context *fc, struct dentry *reference) +{ + struct apparmor_fs_context *afc; + + afc = kzalloc(sizeof(*afc), GFP_KERNEL); + if (!afc) + return -ENOMEM; + + fc->security = afc; + return 0; +} + +static int apparmor_fs_context_dup(struct fs_context *fc, struct fs_context *src_fc) +{ + fc->security = NULL; + return 0; +} + +static void apparmor_fs_context_free(struct fs_context *fc) +{ + struct apparmor_fs_context *afc = fc->security; + + if (afc) { + kfree(afc->saved_options); + kfree(afc); + } +} + +/* + * As a temporary hack, we buffer all the options. The problem is that we need + * to pass them to the DFA evaluator *after* mount point parameters, which + * means deferring the entire check to the sb_mountpoint hook. + */ +static int apparmor_fs_context_parse_option(struct fs_context *fc, char *opt, size_t len) +{ + struct apparmor_fs_context *afc = fc->security; + size_t space = 0; + char *p, *q; + + if (afc->saved_size > 0) + space = 1; + + p = krealloc(afc->saved_options, afc->saved_size + space + len + 1, GFP_KERNEL); + if (!p) + return -ENOMEM; + + q = p + afc->saved_size; + if (q != p) + *q++ = ' '; + memcpy(q, opt, len); + q += len; + *q = 0; + + afc->saved_options = p; + afc->saved_size += 1 + len; + return 0; +} + +static int apparmor_sb_mountpoint(struct fs_context *fc, struct path *mountpoint, + unsigned int mnt_flags) +{ + struct aa_label *label; + int error = 0; + + label = __begin_current_label_crit_section(); + if (!unconfined(label)) + error = aa_new_mount_fc(label, fc, mountpoint); + __end_current_label_crit_section(label); + + return error; +} + static int apparmor_sb_mount(const char *dev_name, const struct path *path, const char *type, unsigned long flags, void *data) { @@ -530,7 +602,7 @@ static int apparmor_sb_mount(const char *dev_name, const struct path *path, if ((flags & MS_MGC_MSK) == MS_MGC_VAL) flags &= ~MS_MGC_MSK; - flags &= ~AA_MS_IGNORE_MASK; + flags &= ~AA_SB_IGNORE_MASK; label = __begin_current_label_crit_section(); if (!unconfined(label)) { @@ -1133,6 +1205,12 @@ static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = { LSM_HOOK_INIT(capget, apparmor_capget), LSM_HOOK_INIT(capable, apparmor_capable), + LSM_HOOK_INIT(fs_context_alloc, apparmor_fs_context_alloc), + LSM_HOOK_INIT(fs_context_dup, apparmor_fs_context_dup), + LSM_HOOK_INIT(fs_context_free, apparmor_fs_context_free), + LSM_HOOK_INIT(fs_context_parse_option, apparmor_fs_context_parse_option), + LSM_HOOK_INIT(sb_mountpoint, apparmor_sb_mountpoint), + LSM_HOOK_INIT(sb_mount, apparmor_sb_mount), LSM_HOOK_INIT(sb_umount, apparmor_sb_umount), LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot), diff --git a/security/apparmor/mount.c b/security/apparmor/mount.c index 8c3787399356..3c95fffb76ac 100644 --- a/security/apparmor/mount.c +++ b/security/apparmor/mount.c @@ -554,6 +554,52 @@ int aa_new_mount(struct aa_label *label, const char *dev_name, return error; } +int aa_new_mount_fc(struct aa_label *label, struct fs_context *fc, + const struct path *mountpoint) +{ + struct apparmor_fs_context *afc = fc->security; + struct aa_profile *profile; + char *buffer = NULL, *dev_buffer = NULL; + bool binary; + int error; + struct path tmp_path, *dev_path = NULL; + + AA_BUG(!label); + AA_BUG(!mountpoint); + + binary = fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA; + + if (fc->fs_type->fs_flags & FS_REQUIRES_DEV) { + if (!fc->source) + return -ENOENT; + + error = kern_path(fc->source, LOOKUP_FOLLOW, &tmp_path); + if (error) + return error; + dev_path = &tmp_path; + } + + get_buffers(buffer, dev_buffer); + if (dev_path) { + error = fn_for_each_confined(label, profile, + match_mnt(profile, mountpoint, buffer, dev_path, dev_buffer, + fc->fs_type->name, + fc->sb_flags & ~AA_SB_IGNORE_MASK, + afc->saved_options, binary)); + } else { + error = fn_for_each_confined(label, profile, + match_mnt_path_str(profile, mountpoint, buffer, + fc->source, fc->fs_type->name, + fc->sb_flags & ~AA_SB_IGNORE_MASK, + afc->saved_options, binary, NULL)); + } + put_buffers(buffer, dev_buffer); + if (dev_path) + path_put(dev_path); + + return error; +} + static int profile_umount(struct aa_profile *profile, struct path *path, char *buffer) {