2011-04-27 12:36:15

by Roberto Sassu

[permalink] [raw]
Subject: [RFC][PATCH 0/7] File descriptor labeling


Attachments:
(No filename) (3.35 kB)
smime.p7s (2.01 kB)
Download all attachments

2011-04-27 15:53:03

by Casey Schaufler

[permalink] [raw]
Subject: Re: [RFC][PATCH 0/7] File descriptor labeling

On 4/27/2011 5:34 AM, Roberto Sassu wrote:
> File descriptor labeling issue
>
> Actually SELinux and SMACK assign to file descriptors the same label of the
> opening process and use it in LSM hooks security_file_permission(),
> security_file_fcntl() and others to verify if the 'current' process has the
> rights to perform the requested operation.
>
> Using the credentials of the 'current' process may be not appropriate in
> case a file descriptor is opened by a kernel service (i.e. a filesystem)
> and made shared among user processes. For instance, in a system with
> SELinux and eCryptfs, if the process A opens an encrypted file, eCryptfs
> obtains a file descriptor to access the correspondent inode in the lower
> filesystem, labeled with the A's label.
>
> If the process B accesses the same encrypted file, it needs the 'use'
> permission on the A's label other than permissions for the lower inode.
> However, if B is the first accessing process, A needs the 'use' permission
> on the B's label.
>
> The solution proposed is to modify those kernel services that deal with
> file descriptors to provide their set of credentials to dentry_open(), so
> that obtained objects are labeled with a unique label. In this way, in the
> above example, if eCryptfs provides its credentials with the label C to
> dentry_open(), all user processes need the 'use' permission only on C.
>
>
>
> File descriptor labeling and IMA
>
> The above proposal suggests to use the file descriptor label as a new
> criteria in the IMA policy to determine if a file must be measured. It will
> be possible to measure all files opened by a kernel service by simply
> writing a rule where the file descriptor label given as a value matches the
> one provided by the same service together with other credentials to the
> function dentry_open().
>
> In the above example, if eCryptfs provides its credentials with the label C
> to dentry_open(), it is possible to measure all inodes opened in the lower
> filesystem by specifying a rule like:
>
> fowner_type=C
>
>
> The benefits of this new criteria will be greater with the integration of
> EVM and the IMA appraisal feature in the kernel. ECryptfs can be used in
> conjunction with these components to verify the integrity of the content
> and extended attributes of encrypted files.
>
> Roberto Sassu
>
>
> Roberto Sassu (7):
> fs: initialize file->f_cred with credentials provided
> selinux: label new file descriptors using file->f_cred
> smack: assign the label set in file->f_cred to new file descriptors
> smack: fix label check in smack_kernel_act_as()
> smack: import the security label in smack_secctx_to_secid()
> security: new LSM hook security_file_getsecid()
> ima: added new LSM conditions in the policy
>
> Documentation/ABI/testing/ima_policy | 7 ++++-
> fs/file_table.c | 5 +--
> fs/internal.h | 2 +-
> fs/namei.c | 2 +-
> fs/open.c | 2 +-
> include/linux/security.h | 12 +++++++++
> security/capability.c | 6 ++++
> security/integrity/ima/ima.h | 4 +-
> security/integrity/ima/ima_api.c | 4 +-
> security/integrity/ima/ima_main.c | 4 +-
> security/integrity/ima/ima_policy.c | 45 +++++++++++++++++++++++++++++----
> security/security.c | 6 ++++
> security/selinux/hooks.c | 9 ++++++-
> security/smack/smack_lsm.c | 23 +++++++++++++++--
> 14 files changed, 108 insertions(+), 23 deletions(-)

I have not given this patch set a complete review, but at first
glance I do not like it. You seem to be making a lot of assumptions
about the interactions between services and LSMs that I don't
know are valid. It is possible that I have not dug in deeply
enough to understand where this is headed. I will have a more
complete review in a bit.

2011-04-27 20:20:03

by Casey Schaufler

[permalink] [raw]
Subject: Re: [RFC][PATCH 0/7] File descriptor labeling

On 4/27/2011 5:34 AM, Roberto Sassu wrote:
> File descriptor labeling issue
>
> Actually SELinux and SMACK assign to file descriptors the same label of the
> opening process and use it in LSM hooks security_file_permission(),
> security_file_fcntl() and others to verify if the 'current' process has the
> rights to perform the requested operation.
>
> Using the credentials of the 'current' process may be not appropriate in
> case a file descriptor is opened by a kernel service (i.e. a filesystem)
> and made shared among user processes. For instance, in a system with
> SELinux and eCryptfs, if the process A opens an encrypted file, eCryptfs
> obtains a file descriptor to access the correspondent inode in the lower
> filesystem, labeled with the A's label.
>
> If the process B accesses the same encrypted file, it needs the 'use'
> permission on the A's label other than permissions for the lower inode.
> However, if B is the first accessing process, A needs the 'use' permission
> on the B's label.

I am having trouble understanding the argument. I will pose my
question in Smack terms, as I can speak most definitively in them.

A process running with a Smack label "A" creates a file, and that
file gets labeled "A", as it ought. If eCryptfs is behaving correctly
this ought not change. If eCryptfs in encrypting the label it needs
to do so in such a way as to be able to decrypt it prior to
presentation to the vfs layer, where it will be used in an access
check. When the process running with a Smack label "B" comes along
the vfs code will check the fetched and possibly decrypted "A"
against "B" and, unless there is an explicit Smack rule in place
granting "B" access to "A", fail.

What is the problem? What is eCryptfs doing that prevents this
from working?

2011-04-27 23:22:40

by Casey Schaufler

[permalink] [raw]
Subject: Re: [RFC][PATCH 4/7] smack: fix label check in smack_kernel_act_as()

On 4/27/2011 5:34 AM, Roberto Sassu wrote:
> The function smack_kernel_act_as() must return -EINVAL if the label
> returned by smack_from_secid() is equal to 'smack_known_invalid.smk_known',
> which means that no entries in the 'smack_known_list' list matching the
> security identifier given are found.

I'll admit that the code here is wrong, but I disagree with the fix.

smack_from_secid() will never return NULL, so the check for NULL is
pointless. Checking for known_invalid is not right either, as the
Smack philosophy is to return a label in all cases, as is evident
by the behavior of smack_from_secid(). Thus, the correct change
would be to remove the error check completely and set the new task
value to the value obtained from smack_from_secid in all cases.

Besides, where did the caller of this function get a secid that isn't
going to map to a Smack label?

> Signed-off-by: Roberto Sassu <[email protected]>
> ---
> security/smack/smack_lsm.c | 2 +-
> 1 files changed, 1 insertions(+), 1 deletions(-)
>
> diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
> index e3c9e54..0e7ed31 100644
> --- a/security/smack/smack_lsm.c
> +++ b/security/smack/smack_lsm.c
> @@ -1415,7 +1415,7 @@ static int smack_kernel_act_as(struct cred *new, u32 secid)
> struct task_smack *new_tsp = new->security;
> char *smack = smack_from_secid(secid);
>
> - if (smack == NULL)
> + if (smack == smack_known_invalid.smk_known)
> return -EINVAL;
>
> new_tsp->smk_task = smack;

2011-04-27 23:26:48

by Casey Schaufler

[permalink] [raw]
Subject: Re: [RFC][PATCH 3/7] smack: assign the label set in file->f_cred to new file descriptors

On 4/27/2011 5:34 AM, Roberto Sassu wrote:
> The SMACK label of new file descriptors is obtained from the credentials
> set in the 'f_cred' field of the same structure.
>
> Signed-off-by: Roberto Sassu <[email protected]>
> ---
> security/smack/smack_lsm.c | 2 +-
> 1 files changed, 1 insertions(+), 1 deletions(-)
>
> diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
> index c6f8fca..e3c9e54 100644
> --- a/security/smack/smack_lsm.c
> +++ b/security/smack/smack_lsm.c
> @@ -1011,7 +1011,7 @@ static int smack_file_permission(struct file *file, int mask)
> */
> static int smack_file_alloc_security(struct file *file)
> {
> - file->f_security = smk_of_current();
> + file->f_security = smk_of_task(file->f_cred->security);

Now hang on. This just looks wrong. You're setting the value of one
field of the file structure to another value in the same file structure.
I don't see that this is what I want.

> return 0;
> }
>

2011-04-27 23:27:30

by Tyler Hicks

[permalink] [raw]
Subject: Re: [RFC][PATCH 0/7] File descriptor labeling

On Wed Apr 27, 2011 at 01:19:55PM -0700, Casey Schaufler <[email protected]> wrote:
> On 4/27/2011 5:34 AM, Roberto Sassu wrote:
> > File descriptor labeling issue
> >
> > Actually SELinux and SMACK assign to file descriptors the same label of the
> > opening process and use it in LSM hooks security_file_permission(),
> > security_file_fcntl() and others to verify if the 'current' process has the
> > rights to perform the requested operation.
> >
> > Using the credentials of the 'current' process may be not appropriate in
> > case a file descriptor is opened by a kernel service (i.e. a filesystem)
> > and made shared among user processes. For instance, in a system with
> > SELinux and eCryptfs, if the process A opens an encrypted file, eCryptfs
> > obtains a file descriptor to access the correspondent inode in the lower
> > filesystem, labeled with the A's label.
> >
> > If the process B accesses the same encrypted file, it needs the 'use'
> > permission on the A's label other than permissions for the lower inode.
> > However, if B is the first accessing process, A needs the 'use' permission
> > on the B's label.
>
> I am having trouble understanding the argument. I will pose my
> question in Smack terms, as I can speak most definitively in them.
>
> A process running with a Smack label "A" creates a file, and that
> file gets labeled "A", as it ought. If eCryptfs is behaving correctly
> this ought not change. If eCryptfs in encrypting the label it needs
> to do so in such a way as to be able to decrypt it prior to
> presentation to the vfs layer, where it will be used in an access
> check. When the process running with a Smack label "B" comes along
> the vfs code will check the fetched and possibly decrypted "A"
> against "B" and, unless there is an explicit Smack rule in place
> granting "B" access to "A", fail.
>
> What is the problem? What is eCryptfs doing that prevents this
> from working?

Hi Casey - I think what Roberto is getting at is the way eCryptfs uses
only one lower file per eCryptfs inode. Imagine that there are 5
files open for ~/secret/foo at the eCryptfs layer, only 1 file is going
to be open in the lower filesystem and all eCryptfs file operations will
be multiplexed through it.

To make things more complicated, if the eCryptfs file is opened for
writing, the lower file must be opened for reading and writing. This is
because a write operation requires eCryptfs to vfs_read() from the lower
filesystem, decrypt that data and then vfs_write() the new data.

If the lower file can't be opened O_RDWR by the calling process, the
request is handed off to a kernel thread to open the lower file on
behalf of the calling process. It is definitely ugly.

Roberto, I hope I correctly described the situation that you're trying
to address. Can you tell me why we can't have a 1:1 mapping of eCryptfs
files to lower files?

Instead of having just one lower file attached to the eCryptfs inode, we
could have a list of opened files. There would be one for each eCryptfs
file that was opened. ecryptfs_writepage() would have to pick, in a
somewhat random fashion, one of the lower files to use. Of course, we
would still need to solve the problem of opening the lower file O_RDWR
when the calling process is only allowed write access (I may have just
answered my own question of why the 1:1 mapping technique won't solve
this problem).

Tyler

2011-04-27 23:47:13

by Casey Schaufler

[permalink] [raw]
Subject: Re: [RFC][PATCH 5/7] smack: import the security label in smack_secctx_to_secid()

On 4/27/2011 5:34 AM, Roberto Sassu wrote:
> The security label passed as argument in smack_secctx_to_secid() must be
> first imported in the 'smack_known_list' list before finding the security
> identifier associated to it. This allows the function
> set_security_override_from_ctx() to be performed successfully even if the
> label is not yet present in the smack list.
>
> Signed-off-by: Roberto Sassu <[email protected]>

Nacked-by: Casey Schaufler <[email protected]>

security_secctx_to_secid() is called from the netlabel code
and hence cannot (to my understanding) allocate memory,
which is something that smk_import will do if the label is
new. I am willing to be convinced that doing so is safe,
but my understanding is that it is not.

> ---
> security/smack/smack_lsm.c | 8 +++++++-
> 1 files changed, 7 insertions(+), 1 deletions(-)
>
> diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
> index 0e7ed31..6612ba1 100644
> --- a/security/smack/smack_lsm.c
> +++ b/security/smack/smack_lsm.c
> @@ -3346,7 +3346,13 @@ static int smack_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
> */
> static int smack_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
> {
> - *secid = smack_to_secid(secdata);
> + char *smack;
> +
> + smack = smk_import(secdata, seclen);
> + if (smack == NULL)
> + return -EINVAL;
> +
> + *secid = smack_to_secid(smack);
> return 0;
> }
>

2011-04-27 23:51:03

by Casey Schaufler

[permalink] [raw]
Subject: Re: [RFC][PATCH 6/7] security: new LSM hook security_file_getsecid()

On 4/27/2011 5:34 AM, Roberto Sassu wrote:
> The new LSM hook security_file_getsecid() and its implementation in the
> capability module, SELinux and SMACK allows to obtain the security
> identifier associated to a file descriptor.

Why do you want a secid? You have the security information from
the file structure readily available.

What use to you intend to put this to?

> Signed-off-by: Roberto Sassu <[email protected]>
> ---
> include/linux/security.h | 12 ++++++++++++
> security/capability.c | 6 ++++++
> security/security.c | 6 ++++++
> security/selinux/hooks.c | 7 +++++++
> security/smack/smack_lsm.c | 11 +++++++++++
> 5 files changed, 42 insertions(+), 0 deletions(-)
>
> diff --git a/include/linux/security.h b/include/linux/security.h
> index ca02f17..6e73a1a 100644
> --- a/include/linux/security.h
> +++ b/include/linux/security.h
> @@ -630,6 +630,11 @@ static inline void security_free_mnt_opts(struct security_mnt_opts *opts)
> * to receive an open file descriptor via socket IPC.
> * @file contains the file structure being received.
> * Return 0 if permission is granted.
> + * @file_getsecid:
> + * Get the secid associated with the file descriptor.
> + * @file contains a pointer to the file descriptor.
> + * @secid contains a pointer to the location where result will be saved.
> + * In case of failure, @secid will be set to zero.
> *
> * Security hook for dentry
> *
> @@ -1492,6 +1497,7 @@ struct security_operations {
> int (*file_send_sigiotask) (struct task_struct *tsk,
> struct fown_struct *fown, int sig);
> int (*file_receive) (struct file *file);
> + void (*file_getsecid)(const struct file *file, u32 *secid);
> int (*dentry_open) (struct file *file, const struct cred *cred);
>
> int (*task_create) (unsigned long clone_flags);
> @@ -1751,6 +1757,7 @@ int security_file_set_fowner(struct file *file);
> int security_file_send_sigiotask(struct task_struct *tsk,
> struct fown_struct *fown, int sig);
> int security_file_receive(struct file *file);
> +void security_file_getsecid(const struct file *file, u32 *secid);
> int security_dentry_open(struct file *file, const struct cred *cred);
> int security_task_create(unsigned long clone_flags);
> int security_cred_alloc_blank(struct cred *cred, gfp_t gfp);
> @@ -2251,6 +2258,11 @@ static inline int security_file_receive(struct file *file)
> return 0;
> }
>
> +static inline void security_file_getsecid(const struct file *file, u32 *secid)
> +{
> + *secid = 0;
> +}
> +
> static inline int security_dentry_open(struct file *file,
> const struct cred *cred)
> {
> diff --git a/security/capability.c b/security/capability.c
> index 2984ea4..fcb569d 100644
> --- a/security/capability.c
> +++ b/security/capability.c
> @@ -349,6 +349,11 @@ static int cap_file_receive(struct file *file)
> return 0;
> }
>
> +static void cap_file_getsecid(const struct file *file, u32 *secid)
> +{
> + *secid = 0;
> +}
> +
> static int cap_dentry_open(struct file *file, const struct cred *cred)
> {
> return 0;
> @@ -953,6 +958,7 @@ void __init security_fixup_ops(struct security_operations *ops)
> set_to_cap_if_null(ops, file_set_fowner);
> set_to_cap_if_null(ops, file_send_sigiotask);
> set_to_cap_if_null(ops, file_receive);
> + set_to_cap_if_null(ops, file_getsecid);
> set_to_cap_if_null(ops, dentry_open);
> set_to_cap_if_null(ops, task_create);
> set_to_cap_if_null(ops, cred_alloc_blank);
> diff --git a/security/security.c b/security/security.c
> index 1011423..9973dab 100644
> --- a/security/security.c
> +++ b/security/security.c
> @@ -688,6 +688,12 @@ int security_file_receive(struct file *file)
> return security_ops->file_receive(file);
> }
>
> +void security_file_getsecid(const struct file *file, u32 *secid)
> +{
> + security_ops->file_getsecid(file, secid);
> +}
> +EXPORT_SYMBOL(security_file_getsecid);
> +
> int security_dentry_open(struct file *file, const struct cred *cred)
> {
> int ret;
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index 6772687..e1e787c 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -3179,6 +3179,12 @@ static int selinux_file_receive(struct file *file)
> return file_has_perm(cred, file, file_to_av(file));
> }
>
> +static void selinux_file_getsecid(const struct file *file, u32 *secid)
> +{
> + struct file_security_struct *fsec = file->f_security;
> + *secid = fsec->sid;
> +}
> +
> static int selinux_dentry_open(struct file *file, const struct cred *cred)
> {
> struct file_security_struct *fsec;
> @@ -5498,6 +5504,7 @@ static struct security_operations selinux_ops = {
> .file_set_fowner = selinux_file_set_fowner,
> .file_send_sigiotask = selinux_file_send_sigiotask,
> .file_receive = selinux_file_receive,
> + .file_getsecid = selinux_file_getsecid,
>
> .dentry_open = selinux_dentry_open,
>
> diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
> index 6612ba1..a583736 100644
> --- a/security/smack/smack_lsm.c
> +++ b/security/smack/smack_lsm.c
> @@ -1304,6 +1304,16 @@ static int smack_file_receive(struct file *file)
> return smk_curacc(file->f_security, may, &ad);
> }
>
> +/**
> + * smack_file_getsecid - Extract file descriptor's security id
> + * @file: file descriptor to extract the info from
> + * @secid: where result will be saved
> + */
> +static void smack_file_getsecid(const struct file *file, u32 *secid)
> +{
> + *secid = smack_to_secid(file->f_security);
> +}
> +
> /*
> * Task hooks
> */
> @@ -3434,6 +3444,7 @@ struct security_operations smack_ops = {
> .file_set_fowner = smack_file_set_fowner,
> .file_send_sigiotask = smack_file_send_sigiotask,
> .file_receive = smack_file_receive,
> + .file_getsecid = smack_file_getsecid,
>
> .cred_alloc_blank = smack_cred_alloc_blank,
> .cred_free = smack_cred_free,

2011-04-27 23:57:49

by Casey Schaufler

[permalink] [raw]
Subject: Re: [RFC][PATCH 0/7] File descriptor labeling

On 4/27/2011 4:27 PM, Tyler Hicks wrote:
> On Wed Apr 27, 2011 at 01:19:55PM -0700, Casey Schaufler <[email protected]> wrote:
>> On 4/27/2011 5:34 AM, Roberto Sassu wrote:
>>> File descriptor labeling issue
>>>
>>> Actually SELinux and SMACK assign to file descriptors the same label of the
>>> opening process and use it in LSM hooks security_file_permission(),
>>> security_file_fcntl() and others to verify if the 'current' process has the
>>> rights to perform the requested operation.
>>>
>>> Using the credentials of the 'current' process may be not appropriate in
>>> case a file descriptor is opened by a kernel service (i.e. a filesystem)
>>> and made shared among user processes. For instance, in a system with
>>> SELinux and eCryptfs, if the process A opens an encrypted file, eCryptfs
>>> obtains a file descriptor to access the correspondent inode in the lower
>>> filesystem, labeled with the A's label.
>>>
>>> If the process B accesses the same encrypted file, it needs the 'use'
>>> permission on the A's label other than permissions for the lower inode.
>>> However, if B is the first accessing process, A needs the 'use' permission
>>> on the B's label.
>> I am having trouble understanding the argument. I will pose my
>> question in Smack terms, as I can speak most definitively in them.
>>
>> A process running with a Smack label "A" creates a file, and that
>> file gets labeled "A", as it ought. If eCryptfs is behaving correctly
>> this ought not change. If eCryptfs in encrypting the label it needs
>> to do so in such a way as to be able to decrypt it prior to
>> presentation to the vfs layer, where it will be used in an access
>> check. When the process running with a Smack label "B" comes along
>> the vfs code will check the fetched and possibly decrypted "A"
>> against "B" and, unless there is an explicit Smack rule in place
>> granting "B" access to "A", fail.
>>
>> What is the problem? What is eCryptfs doing that prevents this
>> from working?
> Hi Casey - I think what Roberto is getting at is the way eCryptfs uses
> only one lower file per eCryptfs inode. Imagine that there are 5
> files open for ~/secret/foo at the eCryptfs layer, only 1 file is going
> to be open in the lower filesystem and all eCryptfs file operations will
> be multiplexed through it.
>
> To make things more complicated, if the eCryptfs file is opened for
> writing, the lower file must be opened for reading and writing. This is
> because a write operation requires eCryptfs to vfs_read() from the lower
> filesystem, decrypt that data and then vfs_write() the new data.
>
> If the lower file can't be opened O_RDWR by the calling process, the
> request is handed off to a kernel thread to open the lower file on
> behalf of the calling process. It is definitely ugly.

Is eCryptfs handling xattrs? It needs to be if it isn't.

> Roberto, I hope I correctly described the situation that you're trying
> to address. Can you tell me why we can't have a 1:1 mapping of eCryptfs
> files to lower files?
>
> Instead of having just one lower file attached to the eCryptfs inode, we
> could have a list of opened files. There would be one for each eCryptfs
> file that was opened. ecryptfs_writepage() would have to pick, in a
> somewhat random fashion, one of the lower files to use. Of course, we
> would still need to solve the problem of opening the lower file O_RDWR
> when the calling process is only allowed write access (I may have just
> answered my own question of why the 1:1 mapping technique won't solve
> this problem).
>
> Tyler
>
>

2011-04-28 00:07:00

by Tyler Hicks

[permalink] [raw]
Subject: Re: [RFC][PATCH 0/7] File descriptor labeling

On Wed Apr 27, 2011 at 04:57:42PM -0700, Casey Schaufler <[email protected]> wrote:
> On 4/27/2011 4:27 PM, Tyler Hicks wrote:
> > On Wed Apr 27, 2011 at 01:19:55PM -0700, Casey Schaufler <[email protected]> wrote:
> >> On 4/27/2011 5:34 AM, Roberto Sassu wrote:
> >>> File descriptor labeling issue
> >>>
> >>> Actually SELinux and SMACK assign to file descriptors the same label of the
> >>> opening process and use it in LSM hooks security_file_permission(),
> >>> security_file_fcntl() and others to verify if the 'current' process has the
> >>> rights to perform the requested operation.
> >>>
> >>> Using the credentials of the 'current' process may be not appropriate in
> >>> case a file descriptor is opened by a kernel service (i.e. a filesystem)
> >>> and made shared among user processes. For instance, in a system with
> >>> SELinux and eCryptfs, if the process A opens an encrypted file, eCryptfs
> >>> obtains a file descriptor to access the correspondent inode in the lower
> >>> filesystem, labeled with the A's label.
> >>>
> >>> If the process B accesses the same encrypted file, it needs the 'use'
> >>> permission on the A's label other than permissions for the lower inode.
> >>> However, if B is the first accessing process, A needs the 'use' permission
> >>> on the B's label.
> >> I am having trouble understanding the argument. I will pose my
> >> question in Smack terms, as I can speak most definitively in them.
> >>
> >> A process running with a Smack label "A" creates a file, and that
> >> file gets labeled "A", as it ought. If eCryptfs is behaving correctly
> >> this ought not change. If eCryptfs in encrypting the label it needs
> >> to do so in such a way as to be able to decrypt it prior to
> >> presentation to the vfs layer, where it will be used in an access
> >> check. When the process running with a Smack label "B" comes along
> >> the vfs code will check the fetched and possibly decrypted "A"
> >> against "B" and, unless there is an explicit Smack rule in place
> >> granting "B" access to "A", fail.
> >>
> >> What is the problem? What is eCryptfs doing that prevents this
> >> from working?
> > Hi Casey - I think what Roberto is getting at is the way eCryptfs uses
> > only one lower file per eCryptfs inode. Imagine that there are 5
> > files open for ~/secret/foo at the eCryptfs layer, only 1 file is going
> > to be open in the lower filesystem and all eCryptfs file operations will
> > be multiplexed through it.
> >
> > To make things more complicated, if the eCryptfs file is opened for
> > writing, the lower file must be opened for reading and writing. This is
> > because a write operation requires eCryptfs to vfs_read() from the lower
> > filesystem, decrypt that data and then vfs_write() the new data.
> >
> > If the lower file can't be opened O_RDWR by the calling process, the
> > request is handed off to a kernel thread to open the lower file on
> > behalf of the calling process. It is definitely ugly.
>
> Is eCryptfs handling xattrs? It needs to be if it isn't.

Yes - we pass the calls to the lower filesystem and return the result.

The only catch would be that users can opt to have eCryptfs store the
crypto metadata in user.ecryptfs instead of the first 8k of the file.

Tyler

>
> > Roberto, I hope I correctly described the situation that you're trying
> > to address. Can you tell me why we can't have a 1:1 mapping of eCryptfs
> > files to lower files?
> >
> > Instead of having just one lower file attached to the eCryptfs inode, we
> > could have a list of opened files. There would be one for each eCryptfs
> > file that was opened. ecryptfs_writepage() would have to pick, in a
> > somewhat random fashion, one of the lower files to use. Of course, we
> > would still need to solve the problem of opening the lower file O_RDWR
> > when the calling process is only allowed write access (I may have just
> > answered my own question of why the 1:1 mapping technique won't solve
> > this problem).
> >
> > Tyler
> >
> >
>

2011-04-28 08:08:42

by Roberto Sassu

[permalink] [raw]
Subject: Re: [RFC][PATCH 3/7] smack: assign the label set in file->f_cred to new file descriptors

On Thursday, April 28, 2011 01:26:43 AM Casey Schaufler wrote:
> On 4/27/2011 5:34 AM, Roberto Sassu wrote:
> > The SMACK label of new file descriptors is obtained from the credentials
> > set in the 'f_cred' field of the same structure.
> >
> > Signed-off-by: Roberto Sassu <[email protected]>
> > ---
> > security/smack/smack_lsm.c | 2 +-
> > 1 files changed, 1 insertions(+), 1 deletions(-)
> >
> > diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
> > index c6f8fca..e3c9e54 100644
> > --- a/security/smack/smack_lsm.c
> > +++ b/security/smack/smack_lsm.c
> > @@ -1011,7 +1011,7 @@ static int smack_file_permission(struct file *file, int mask)
> > */
> > static int smack_file_alloc_security(struct file *file)
> > {
> > - file->f_security = smk_of_current();
> > + file->f_security = smk_of_task(file->f_cred->security);
>
> Now hang on. This just looks wrong. You're setting the value of one
> field of the file structure to another value in the same file structure.
> I don't see that this is what I want.
>

Hi Casey

thanks for the review.
The field 'f_cred' stores the credentials of the subject that issued the open.
The first patch allows to set this field with the credentials provided to
the function get_empty_filp() which may be those of the 'current'
process as in the original case, or those provided by a kernel service that
called the function dentry_open() directly.

Roberto Sassu


> > return 0;
> > }
> >
>
>

2011-04-28 09:24:38

by Roberto Sassu

[permalink] [raw]
Subject: Re: [RFC][PATCH 4/7] smack: fix label check in smack_kernel_act_as()

On Thursday, April 28, 2011 01:22:34 AM Casey Schaufler wrote:
> On 4/27/2011 5:34 AM, Roberto Sassu wrote:
> > The function smack_kernel_act_as() must return -EINVAL if the label
> > returned by smack_from_secid() is equal to 'smack_known_invalid.smk_known',
> > which means that no entries in the 'smack_known_list' list matching the
> > security identifier given are found.
>
> I'll admit that the code here is wrong, but I disagree with the fix.
>
> smack_from_secid() will never return NULL, so the check for NULL is
> pointless. Checking for known_invalid is not right either, as the
> Smack philosophy is to return a label in all cases, as is evident
> by the behavior of smack_from_secid(). Thus, the correct change
> would be to remove the error check completely and set the new task
> value to the value obtained from smack_from_secid in all cases.
>
> Besides, where did the caller of this function get a secid that isn't
> going to map to a Smack label?
>

I discovered the issue when calling the function set_security_override_from_ctx(),
located in kernel/cred.c, which will be used in eCryptfs to override the set
of initial credentials (i'm about to post the patches).
This function accepts an arbitrary string and uses it as a security label to replace
the one in the credentials provided. If the string passed is not yet imported
to SMACK, the 'smack_known_invalid.smk_known' label is applied without this fact
is notified to the caller.
I tried to solve the problem by importing the string during the execution of the
security_secctx_to_secid() hook, so that set_security_override() will never fail.
So, probably the string must be already been imported if no new memory can be
allocated during smack_secctx_to_secid().


> > Signed-off-by: Roberto Sassu <[email protected]>
> > ---
> > security/smack/smack_lsm.c | 2 +-
> > 1 files changed, 1 insertions(+), 1 deletions(-)
> >
> > diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
> > index e3c9e54..0e7ed31 100644
> > --- a/security/smack/smack_lsm.c
> > +++ b/security/smack/smack_lsm.c
> > @@ -1415,7 +1415,7 @@ static int smack_kernel_act_as(struct cred *new, u32 secid)
> > struct task_smack *new_tsp = new->security;
> > char *smack = smack_from_secid(secid);
> >
> > - if (smack == NULL)
> > + if (smack == smack_known_invalid.smk_known)
> > return -EINVAL;
> >
> > new_tsp->smk_task = smack;
>
>

2011-04-28 09:44:15

by Roberto Sassu

[permalink] [raw]
Subject: Re: [RFC][PATCH 6/7] security: new LSM hook security_file_getsecid()

On Thursday, April 28, 2011 01:50:58 AM Casey Schaufler wrote:
> On 4/27/2011 5:34 AM, Roberto Sassu wrote:
> > The new LSM hook security_file_getsecid() and its implementation in the
> > capability module, SELinux and SMACK allows to obtain the security
> > identifier associated to a file descriptor.
>
> Why do you want a secid? You have the security information from
> the file structure readily available.
>
> What use to you intend to put this to?
>

This new hook will be used in the IMA code (patch 7/7) to find inodes that
must be measured by checking the security label applied to file descriptors.
This new criteria allows to easily measure all inodes opened by a kernel
service because their file descriptors have the same label of what the
service provided with other credentials to the function dentry_open().


> > Signed-off-by: Roberto Sassu <[email protected]>
> > ---
> > include/linux/security.h | 12 ++++++++++++
> > security/capability.c | 6 ++++++
> > security/security.c | 6 ++++++
> > security/selinux/hooks.c | 7 +++++++
> > security/smack/smack_lsm.c | 11 +++++++++++
> > 5 files changed, 42 insertions(+), 0 deletions(-)
> >
> > diff --git a/include/linux/security.h b/include/linux/security.h
> > index ca02f17..6e73a1a 100644
> > --- a/include/linux/security.h
> > +++ b/include/linux/security.h
> > @@ -630,6 +630,11 @@ static inline void security_free_mnt_opts(struct security_mnt_opts *opts)
> > * to receive an open file descriptor via socket IPC.
> > * @file contains the file structure being received.
> > * Return 0 if permission is granted.
> > + * @file_getsecid:
> > + * Get the secid associated with the file descriptor.
> > + * @file contains a pointer to the file descriptor.
> > + * @secid contains a pointer to the location where result will be saved.
> > + * In case of failure, @secid will be set to zero.
> > *
> > * Security hook for dentry
> > *
> > @@ -1492,6 +1497,7 @@ struct security_operations {
> > int (*file_send_sigiotask) (struct task_struct *tsk,
> > struct fown_struct *fown, int sig);
> > int (*file_receive) (struct file *file);
> > + void (*file_getsecid)(const struct file *file, u32 *secid);
> > int (*dentry_open) (struct file *file, const struct cred *cred);
> >
> > int (*task_create) (unsigned long clone_flags);
> > @@ -1751,6 +1757,7 @@ int security_file_set_fowner(struct file *file);
> > int security_file_send_sigiotask(struct task_struct *tsk,
> > struct fown_struct *fown, int sig);
> > int security_file_receive(struct file *file);
> > +void security_file_getsecid(const struct file *file, u32 *secid);
> > int security_dentry_open(struct file *file, const struct cred *cred);
> > int security_task_create(unsigned long clone_flags);
> > int security_cred_alloc_blank(struct cred *cred, gfp_t gfp);
> > @@ -2251,6 +2258,11 @@ static inline int security_file_receive(struct file *file)
> > return 0;
> > }
> >
> > +static inline void security_file_getsecid(const struct file *file, u32 *secid)
> > +{
> > + *secid = 0;
> > +}
> > +
> > static inline int security_dentry_open(struct file *file,
> > const struct cred *cred)
> > {
> > diff --git a/security/capability.c b/security/capability.c
> > index 2984ea4..fcb569d 100644
> > --- a/security/capability.c
> > +++ b/security/capability.c
> > @@ -349,6 +349,11 @@ static int cap_file_receive(struct file *file)
> > return 0;
> > }
> >
> > +static void cap_file_getsecid(const struct file *file, u32 *secid)
> > +{
> > + *secid = 0;
> > +}
> > +
> > static int cap_dentry_open(struct file *file, const struct cred *cred)
> > {
> > return 0;
> > @@ -953,6 +958,7 @@ void __init security_fixup_ops(struct security_operations *ops)
> > set_to_cap_if_null(ops, file_set_fowner);
> > set_to_cap_if_null(ops, file_send_sigiotask);
> > set_to_cap_if_null(ops, file_receive);
> > + set_to_cap_if_null(ops, file_getsecid);
> > set_to_cap_if_null(ops, dentry_open);
> > set_to_cap_if_null(ops, task_create);
> > set_to_cap_if_null(ops, cred_alloc_blank);
> > diff --git a/security/security.c b/security/security.c
> > index 1011423..9973dab 100644
> > --- a/security/security.c
> > +++ b/security/security.c
> > @@ -688,6 +688,12 @@ int security_file_receive(struct file *file)
> > return security_ops->file_receive(file);
> > }
> >
> > +void security_file_getsecid(const struct file *file, u32 *secid)
> > +{
> > + security_ops->file_getsecid(file, secid);
> > +}
> > +EXPORT_SYMBOL(security_file_getsecid);
> > +
> > int security_dentry_open(struct file *file, const struct cred *cred)
> > {
> > int ret;
> > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> > index 6772687..e1e787c 100644
> > --- a/security/selinux/hooks.c
> > +++ b/security/selinux/hooks.c
> > @@ -3179,6 +3179,12 @@ static int selinux_file_receive(struct file *file)
> > return file_has_perm(cred, file, file_to_av(file));
> > }
> >
> > +static void selinux_file_getsecid(const struct file *file, u32 *secid)
> > +{
> > + struct file_security_struct *fsec = file->f_security;
> > + *secid = fsec->sid;
> > +}
> > +
> > static int selinux_dentry_open(struct file *file, const struct cred *cred)
> > {
> > struct file_security_struct *fsec;
> > @@ -5498,6 +5504,7 @@ static struct security_operations selinux_ops = {
> > .file_set_fowner = selinux_file_set_fowner,
> > .file_send_sigiotask = selinux_file_send_sigiotask,
> > .file_receive = selinux_file_receive,
> > + .file_getsecid = selinux_file_getsecid,
> >
> > .dentry_open = selinux_dentry_open,
> >
> > diff --git a/security/smack/smack_lsm.c b/security/smack/smack_lsm.c
> > index 6612ba1..a583736 100644
> > --- a/security/smack/smack_lsm.c
> > +++ b/security/smack/smack_lsm.c
> > @@ -1304,6 +1304,16 @@ static int smack_file_receive(struct file *file)
> > return smk_curacc(file->f_security, may, &ad);
> > }
> >
> > +/**
> > + * smack_file_getsecid - Extract file descriptor's security id
> > + * @file: file descriptor to extract the info from
> > + * @secid: where result will be saved
> > + */
> > +static void smack_file_getsecid(const struct file *file, u32 *secid)
> > +{
> > + *secid = smack_to_secid(file->f_security);
> > +}
> > +
> > /*
> > * Task hooks
> > */
> > @@ -3434,6 +3444,7 @@ struct security_operations smack_ops = {
> > .file_set_fowner = smack_file_set_fowner,
> > .file_send_sigiotask = smack_file_send_sigiotask,
> > .file_receive = smack_file_receive,
> > + .file_getsecid = smack_file_getsecid,
> >
> > .cred_alloc_blank = smack_cred_alloc_blank,
> > .cred_free = smack_cred_free,
>
>

2011-04-28 12:37:20

by Roberto Sassu

[permalink] [raw]
Subject: Re: [RFC][PATCH 0/7] File descriptor labeling

On Thursday, April 28, 2011 01:27:19 AM Tyler Hicks wrote:
> On Wed Apr 27, 2011 at 01:19:55PM -0700, Casey Schaufler <[email protected]> wrote:
> > On 4/27/2011 5:34 AM, Roberto Sassu wrote:
> > > File descriptor labeling issue
> > >
> > > Actually SELinux and SMACK assign to file descriptors the same label of the
> > > opening process and use it in LSM hooks security_file_permission(),
> > > security_file_fcntl() and others to verify if the 'current' process has the
> > > rights to perform the requested operation.
> > >
> > > Using the credentials of the 'current' process may be not appropriate in
> > > case a file descriptor is opened by a kernel service (i.e. a filesystem)
> > > and made shared among user processes. For instance, in a system with
> > > SELinux and eCryptfs, if the process A opens an encrypted file, eCryptfs
> > > obtains a file descriptor to access the correspondent inode in the lower
> > > filesystem, labeled with the A's label.
> > >
> > > If the process B accesses the same encrypted file, it needs the 'use'
> > > permission on the A's label other than permissions for the lower inode.
> > > However, if B is the first accessing process, A needs the 'use' permission
> > > on the B's label.
> >
> > I am having trouble understanding the argument. I will pose my
> > question in Smack terms, as I can speak most definitively in them.
> >
> > A process running with a Smack label "A" creates a file, and that
> > file gets labeled "A", as it ought. If eCryptfs is behaving correctly
> > this ought not change. If eCryptfs in encrypting the label it needs
> > to do so in such a way as to be able to decrypt it prior to
> > presentation to the vfs layer, where it will be used in an access
> > check. When the process running with a Smack label "B" comes along
> > the vfs code will check the fetched and possibly decrypted "A"
> > against "B" and, unless there is an explicit Smack rule in place
> > granting "B" access to "A", fail.
> >
> > What is the problem? What is eCryptfs doing that prevents this
> > from working?
>
> Hi Casey - I think what Roberto is getting at is the way eCryptfs uses
> only one lower file per eCryptfs inode. Imagine that there are 5
> files open for ~/secret/foo at the eCryptfs layer, only 1 file is going
> to be open in the lower filesystem and all eCryptfs file operations will
> be multiplexed through it.
>
> To make things more complicated, if the eCryptfs file is opened for
> writing, the lower file must be opened for reading and writing. This is
> because a write operation requires eCryptfs to vfs_read() from the lower
> filesystem, decrypt that data and then vfs_write() the new data.
>
> If the lower file can't be opened O_RDWR by the calling process, the
> request is handed off to a kernel thread to open the lower file on
> behalf of the calling process. It is definitely ugly.
>
> Roberto, I hope I correctly described the situation that you're trying
> to address. Can you tell me why we can't have a 1:1 mapping of eCryptfs
> files to lower files?
>
> Instead of having just one lower file attached to the eCryptfs inode, we
> could have a list of opened files. There would be one for each eCryptfs
> file that was opened. ecryptfs_writepage() would have to pick, in a
> somewhat random fashion, one of the lower files to use. Of course, we
> would still need to solve the problem of opening the lower file O_RDWR
> when the calling process is only allowed write access (I may have just
> answered my own question of why the 1:1 mapping technique won't solve
> this problem).
>

Hi Tyler

i think the 1:1 mapping isn't necessary at least from the security perspective.
Since eCryptfs is a stacked filesystem access control is performed on
both the upper and the lower layer.
ECryptfs relies on the lower filesystem for the management of extended
attributes, so this means that the security label of both the upper and
the lower inodes is the same (however this is not the current behavior
in SELinux, which assigns the label 'ecryptfs_t' to the upper inode).
In my view, for this reason the access control checks can be performed
only at the upper layer, letting eCryptfs full privileges to access inodes
in the lower filesystem.
This solves the problem of opening the lower file in r/w mode even if only
the read is requested, because at the upper layer the subject is the
accessing process with its own credentials which needs the read permission
and at the lower layer the subject is the eCryptfs kernel module with
unlimited privileges.
The issue i described in the cover letter is related to the label assigned
to the file descriptor obtained by eCryptfs (or another kernel service) when
opening an inode in the lower filesystem, which actually depends on the
first accessing process.
This label is checked against the credentials of the 'current' process in the
hook security_file_permission(), which is triggered by vfs calls (read, write,
readdir) performed on both the upper and the lower inodes.
In SELinux, a process needs the permission to 'use' a opened file descriptor.
So, having a fixed label helps in defining the rule that must be added in the
policy for eCryptfs to ensure it works properly.


PS: i'm adding in CC the Apparmor's mantainer and the mailing list to have
their opinion about the protection offered for the eCryptfs filesystem and
other kernel services. The overall thread is available at the url:

https://lkml.org/lkml/2011/4/27/201

Thanks

Roberto Sassu


> Tyler
>

2011-04-28 13:33:29

by Mimi Zohar

[permalink] [raw]
Subject: Re: [RFC][PATCH 7/7] ima: added new LSM conditions in the policy

On Wed, 2011-04-27 at 14:34 +0200, Roberto Sassu wrote:
> The new parameters 'fowner_user', 'fowner_role' and 'fowner_type' are new
> LSM conditions that allow to measure inodes whose opened file descriptor
> has the label given as a value.
>
> Signed-off-by: Roberto Sassu <[email protected]>

Nice. I really like the 'fowner_' prefix. If you don't object, I'll
change the ima-appraisal keyword from 'owner' to 'fowner' as well.

Acked-by: Mimi Zohar <[email protected]>

thanks,

Mimi

> ---
> Documentation/ABI/testing/ima_policy | 7 ++++-
> security/integrity/ima/ima.h | 4 +-
> security/integrity/ima/ima_api.c | 4 +-
> security/integrity/ima/ima_main.c | 4 +-
> security/integrity/ima/ima_policy.c | 45 +++++++++++++++++++++++++++++----
> 5 files changed, 51 insertions(+), 13 deletions(-)
>
> diff --git a/Documentation/ABI/testing/ima_policy b/Documentation/ABI/testing/ima_policy
> index 6cd6dae..ee49345 100644
> --- a/Documentation/ABI/testing/ima_policy
> +++ b/Documentation/ABI/testing/ima_policy
> @@ -18,7 +18,8 @@ Description:
> condition:= base | lsm
> base: [[func=] [mask=] [fsmagic=] [uid=]]
> lsm: [[subj_user=] [subj_role=] [subj_type=]
> - [obj_user=] [obj_role=] [obj_type=]]
> + [obj_user=] [obj_role=] [obj_type=]
> + [fowner_user=] [fowner_role=] [fowner_type=]]
>
> base: func:= [BPRM_CHECK][FILE_MMAP][FILE_CHECK]
> mask:= [MAY_READ] [MAY_WRITE] [MAY_APPEND] [MAY_EXEC]
> @@ -46,6 +47,10 @@ Description:
> all files mmapped executable in file_mmap, and all files
> open for read by root in do_filp_open.
>
> + LSM conditions starting with obj_ refer to security attributes
> + of inodes while those starting with fowner_ involve file
> + descriptors.
> +
> Examples of LSM specific definitions:
>
> SELinux:
> diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h
> index 08408bd..3a05625 100644
> --- a/security/integrity/ima/ima.h
> +++ b/security/integrity/ima/ima.h
> @@ -110,7 +110,7 @@ struct ima_iint_cache {
> };
>
> /* LIM API function definitions */
> -int ima_must_measure(struct inode *inode, int mask, int function);
> +int ima_must_measure(struct file *file, int mask, int function);
> int ima_collect_measurement(struct ima_iint_cache *iint, struct file *file);
> void ima_store_measurement(struct ima_iint_cache *iint, struct file *file,
> const unsigned char *filename);
> @@ -128,7 +128,7 @@ struct ima_iint_cache *ima_iint_find(struct inode *inode);
> /* IMA policy related functions */
> enum ima_hooks { FILE_CHECK = 1, FILE_MMAP, BPRM_CHECK };
>
> -int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask);
> +int ima_match_policy(struct file *file, enum ima_hooks func, int mask);
> void ima_init_policy(void);
> void ima_update_policy(void);
> ssize_t ima_parse_add_rule(char *);
> diff --git a/security/integrity/ima/ima_api.c b/security/integrity/ima/ima_api.c
> index da36d2c..d815392 100644
> --- a/security/integrity/ima/ima_api.c
> +++ b/security/integrity/ima/ima_api.c
> @@ -108,11 +108,11 @@ err_out:
> * Return 0 to measure. For matching a DONT_MEASURE policy, no policy,
> * or other error, return an error code.
> */
> -int ima_must_measure(struct inode *inode, int mask, int function)
> +int ima_must_measure(struct file *file, int mask, int function)
> {
> int must_measure;
>
> - must_measure = ima_match_policy(inode, function, mask);
> + must_measure = ima_match_policy(file, function, mask);
> return must_measure ? 0 : -EACCES;
> }
>
> diff --git a/security/integrity/ima/ima_main.c b/security/integrity/ima/ima_main.c
> index 39d66dc..9eaca61 100644
> --- a/security/integrity/ima/ima_main.c
> +++ b/security/integrity/ima/ima_main.c
> @@ -65,7 +65,7 @@ static void ima_rdwr_violation_check(struct file *file)
> goto out;
> }
>
> - rc = ima_must_measure(inode, MAY_READ, FILE_CHECK);
> + rc = ima_must_measure(file, MAY_READ, FILE_CHECK);
> if (rc < 0)
> goto out;
>
> @@ -127,7 +127,7 @@ static int process_measurement(struct file *file, const unsigned char *filename,
> if (!ima_initialized || !S_ISREG(inode->i_mode))
> return 0;
>
> - rc = ima_must_measure(inode, mask, function);
> + rc = ima_must_measure(file, mask, function);
> if (rc != 0)
> return rc;
> retry:
> diff --git a/security/integrity/ima/ima_policy.c b/security/integrity/ima/ima_policy.c
> index d661afb..115c2e7 100644
> --- a/security/integrity/ima/ima_policy.c
> +++ b/security/integrity/ima/ima_policy.c
> @@ -27,9 +27,10 @@
>
> enum ima_action { UNKNOWN = -1, DONT_MEASURE = 0, MEASURE };
>
> -#define MAX_LSM_RULES 6
> +#define MAX_LSM_RULES 9
> enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
> - LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
> + LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE,
> + LSM_FOWNER_USER, LSM_FOWNER_ROLE, LSM_FOWNER_TYPE
> };
>
> struct ima_measure_rule_entry {
> @@ -96,9 +97,10 @@ __setup("ima_tcb", default_policy_setup);
> * Returns true on rule match, false on failure.
> */
> static bool ima_match_rules(struct ima_measure_rule_entry *rule,
> - struct inode *inode, enum ima_hooks func, int mask)
> + struct file *file, enum ima_hooks func, int mask)
> {
> struct task_struct *tsk = current;
> + struct inode *inode = file->f_dentry->d_inode;
> int i;
>
> if ((rule->flags & IMA_FUNC) && rule->func != func)
> @@ -112,7 +114,7 @@ static bool ima_match_rules(struct ima_measure_rule_entry *rule,
> return false;
> for (i = 0; i < MAX_LSM_RULES; i++) {
> int rc = 0;
> - u32 osid, sid;
> + u32 osid, sid, fsid;
>
> if (!rule->lsm[i].rule)
> continue;
> @@ -137,6 +139,15 @@ static bool ima_match_rules(struct ima_measure_rule_entry *rule,
> Audit_equal,
> rule->lsm[i].rule,
> NULL);
> + case LSM_FOWNER_USER:
> + case LSM_FOWNER_ROLE:
> + case LSM_FOWNER_TYPE:
> + security_file_getsecid(file, &fsid);
> + rc = security_filter_rule_match(fsid,
> + rule->lsm[i].type,
> + Audit_equal,
> + rule->lsm[i].rule,
> + NULL);
> default:
> break;
> }
> @@ -159,14 +170,14 @@ static bool ima_match_rules(struct ima_measure_rule_entry *rule,
> * as elements in the list are never deleted, nor does the list
> * change.)
> */
> -int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask)
> +int ima_match_policy(struct file *file, enum ima_hooks func, int mask)
> {
> struct ima_measure_rule_entry *entry;
>
> list_for_each_entry(entry, ima_measure, list) {
> bool rc;
>
> - rc = ima_match_rules(entry, inode, func, mask);
> + rc = ima_match_rules(entry, file, func, mask);
> if (rc)
> return entry->action;
> }
> @@ -222,6 +233,7 @@ enum {
> Opt_measure = 1, Opt_dont_measure,
> Opt_obj_user, Opt_obj_role, Opt_obj_type,
> Opt_subj_user, Opt_subj_role, Opt_subj_type,
> + Opt_fowner_user, Opt_fowner_role, Opt_fowner_type,
> Opt_func, Opt_mask, Opt_fsmagic, Opt_uid
> };
>
> @@ -234,6 +246,9 @@ static match_table_t policy_tokens = {
> {Opt_subj_user, "subj_user=%s"},
> {Opt_subj_role, "subj_role=%s"},
> {Opt_subj_type, "subj_type=%s"},
> + {Opt_fowner_user, "fowner_user=%s"},
> + {Opt_fowner_role, "fowner_role=%s"},
> + {Opt_fowner_type, "fowner_type=%s"},
> {Opt_func, "func=%s"},
> {Opt_mask, "mask=%s"},
> {Opt_fsmagic, "fsmagic=%s"},
> @@ -407,6 +422,24 @@ static int ima_parse_rule(char *rule, struct ima_measure_rule_entry *entry)
> LSM_SUBJ_TYPE,
> AUDIT_SUBJ_TYPE);
> break;
> + case Opt_fowner_user:
> + ima_log_string(ab, "fowner_user", args[0].from);
> + result = ima_lsm_rule_init(entry, args[0].from,
> + LSM_FOWNER_USER,
> + AUDIT_SUBJ_USER);
> + break;
> + case Opt_fowner_role:
> + ima_log_string(ab, "fowner_role", args[0].from);
> + result = ima_lsm_rule_init(entry, args[0].from,
> + LSM_FOWNER_ROLE,
> + AUDIT_SUBJ_ROLE);
> + break;
> + case Opt_fowner_type:
> + ima_log_string(ab, "fowner_type", args[0].from);
> + result = ima_lsm_rule_init(entry, args[0].from,
> + LSM_FOWNER_TYPE,
> + AUDIT_SUBJ_TYPE);
> + break;
> case Opt_err:
> ima_log_string(ab, "UNKNOWN", p);
> result = -EINVAL;

2011-04-28 13:55:10

by Roberto Sassu

[permalink] [raw]
Subject: Re: [RFC][PATCH 7/7] ima: added new LSM conditions in the policy

On Thursday, April 28, 2011 03:32:38 PM Mimi Zohar wrote:
> On Wed, 2011-04-27 at 14:34 +0200, Roberto Sassu wrote:
> > The new parameters 'fowner_user', 'fowner_role' and 'fowner_type' are new
> > LSM conditions that allow to measure inodes whose opened file descriptor
> > has the label given as a value.
> >
> > Signed-off-by: Roberto Sassu <[email protected]>
>
> Nice. I really like the 'fowner_' prefix. If you don't object, I'll
> change the ima-appraisal keyword from 'owner' to 'fowner' as well.
>

Hi Mimi

i agree about this change.

Thanks

Roberto Sassu


> Acked-by: Mimi Zohar <[email protected]>
>
> thanks,
>
> Mimi
>
> > ---
> > Documentation/ABI/testing/ima_policy | 7 ++++-
> > security/integrity/ima/ima.h | 4 +-
> > security/integrity/ima/ima_api.c | 4 +-
> > security/integrity/ima/ima_main.c | 4 +-
> > security/integrity/ima/ima_policy.c | 45 +++++++++++++++++++++++++++++----
> > 5 files changed, 51 insertions(+), 13 deletions(-)
> >
> > diff --git a/Documentation/ABI/testing/ima_policy b/Documentation/ABI/testing/ima_policy
> > index 6cd6dae..ee49345 100644
> > --- a/Documentation/ABI/testing/ima_policy
> > +++ b/Documentation/ABI/testing/ima_policy
> > @@ -18,7 +18,8 @@ Description:
> > condition:= base | lsm
> > base: [[func=] [mask=] [fsmagic=] [uid=]]
> > lsm: [[subj_user=] [subj_role=] [subj_type=]
> > - [obj_user=] [obj_role=] [obj_type=]]
> > + [obj_user=] [obj_role=] [obj_type=]
> > + [fowner_user=] [fowner_role=] [fowner_type=]]
> >
> > base: func:= [BPRM_CHECK][FILE_MMAP][FILE_CHECK]
> > mask:= [MAY_READ] [MAY_WRITE] [MAY_APPEND] [MAY_EXEC]
> > @@ -46,6 +47,10 @@ Description:
> > all files mmapped executable in file_mmap, and all files
> > open for read by root in do_filp_open.
> >
> > + LSM conditions starting with obj_ refer to security attributes
> > + of inodes while those starting with fowner_ involve file
> > + descriptors.
> > +
> > Examples of LSM specific definitions:
> >
> > SELinux:
> > diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h
> > index 08408bd..3a05625 100644
> > --- a/security/integrity/ima/ima.h
> > +++ b/security/integrity/ima/ima.h
> > @@ -110,7 +110,7 @@ struct ima_iint_cache {
> > };
> >
> > /* LIM API function definitions */
> > -int ima_must_measure(struct inode *inode, int mask, int function);
> > +int ima_must_measure(struct file *file, int mask, int function);
> > int ima_collect_measurement(struct ima_iint_cache *iint, struct file *file);
> > void ima_store_measurement(struct ima_iint_cache *iint, struct file *file,
> > const unsigned char *filename);
> > @@ -128,7 +128,7 @@ struct ima_iint_cache *ima_iint_find(struct inode *inode);
> > /* IMA policy related functions */
> > enum ima_hooks { FILE_CHECK = 1, FILE_MMAP, BPRM_CHECK };
> >
> > -int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask);
> > +int ima_match_policy(struct file *file, enum ima_hooks func, int mask);
> > void ima_init_policy(void);
> > void ima_update_policy(void);
> > ssize_t ima_parse_add_rule(char *);
> > diff --git a/security/integrity/ima/ima_api.c b/security/integrity/ima/ima_api.c
> > index da36d2c..d815392 100644
> > --- a/security/integrity/ima/ima_api.c
> > +++ b/security/integrity/ima/ima_api.c
> > @@ -108,11 +108,11 @@ err_out:
> > * Return 0 to measure. For matching a DONT_MEASURE policy, no policy,
> > * or other error, return an error code.
> > */
> > -int ima_must_measure(struct inode *inode, int mask, int function)
> > +int ima_must_measure(struct file *file, int mask, int function)
> > {
> > int must_measure;
> >
> > - must_measure = ima_match_policy(inode, function, mask);
> > + must_measure = ima_match_policy(file, function, mask);
> > return must_measure ? 0 : -EACCES;
> > }
> >
> > diff --git a/security/integrity/ima/ima_main.c b/security/integrity/ima/ima_main.c
> > index 39d66dc..9eaca61 100644
> > --- a/security/integrity/ima/ima_main.c
> > +++ b/security/integrity/ima/ima_main.c
> > @@ -65,7 +65,7 @@ static void ima_rdwr_violation_check(struct file *file)
> > goto out;
> > }
> >
> > - rc = ima_must_measure(inode, MAY_READ, FILE_CHECK);
> > + rc = ima_must_measure(file, MAY_READ, FILE_CHECK);
> > if (rc < 0)
> > goto out;
> >
> > @@ -127,7 +127,7 @@ static int process_measurement(struct file *file, const unsigned char *filename,
> > if (!ima_initialized || !S_ISREG(inode->i_mode))
> > return 0;
> >
> > - rc = ima_must_measure(inode, mask, function);
> > + rc = ima_must_measure(file, mask, function);
> > if (rc != 0)
> > return rc;
> > retry:
> > diff --git a/security/integrity/ima/ima_policy.c b/security/integrity/ima/ima_policy.c
> > index d661afb..115c2e7 100644
> > --- a/security/integrity/ima/ima_policy.c
> > +++ b/security/integrity/ima/ima_policy.c
> > @@ -27,9 +27,10 @@
> >
> > enum ima_action { UNKNOWN = -1, DONT_MEASURE = 0, MEASURE };
> >
> > -#define MAX_LSM_RULES 6
> > +#define MAX_LSM_RULES 9
> > enum lsm_rule_types { LSM_OBJ_USER, LSM_OBJ_ROLE, LSM_OBJ_TYPE,
> > - LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE
> > + LSM_SUBJ_USER, LSM_SUBJ_ROLE, LSM_SUBJ_TYPE,
> > + LSM_FOWNER_USER, LSM_FOWNER_ROLE, LSM_FOWNER_TYPE
> > };
> >
> > struct ima_measure_rule_entry {
> > @@ -96,9 +97,10 @@ __setup("ima_tcb", default_policy_setup);
> > * Returns true on rule match, false on failure.
> > */
> > static bool ima_match_rules(struct ima_measure_rule_entry *rule,
> > - struct inode *inode, enum ima_hooks func, int mask)
> > + struct file *file, enum ima_hooks func, int mask)
> > {
> > struct task_struct *tsk = current;
> > + struct inode *inode = file->f_dentry->d_inode;
> > int i;
> >
> > if ((rule->flags & IMA_FUNC) && rule->func != func)
> > @@ -112,7 +114,7 @@ static bool ima_match_rules(struct ima_measure_rule_entry *rule,
> > return false;
> > for (i = 0; i < MAX_LSM_RULES; i++) {
> > int rc = 0;
> > - u32 osid, sid;
> > + u32 osid, sid, fsid;
> >
> > if (!rule->lsm[i].rule)
> > continue;
> > @@ -137,6 +139,15 @@ static bool ima_match_rules(struct ima_measure_rule_entry *rule,
> > Audit_equal,
> > rule->lsm[i].rule,
> > NULL);
> > + case LSM_FOWNER_USER:
> > + case LSM_FOWNER_ROLE:
> > + case LSM_FOWNER_TYPE:
> > + security_file_getsecid(file, &fsid);
> > + rc = security_filter_rule_match(fsid,
> > + rule->lsm[i].type,
> > + Audit_equal,
> > + rule->lsm[i].rule,
> > + NULL);
> > default:
> > break;
> > }
> > @@ -159,14 +170,14 @@ static bool ima_match_rules(struct ima_measure_rule_entry *rule,
> > * as elements in the list are never deleted, nor does the list
> > * change.)
> > */
> > -int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask)
> > +int ima_match_policy(struct file *file, enum ima_hooks func, int mask)
> > {
> > struct ima_measure_rule_entry *entry;
> >
> > list_for_each_entry(entry, ima_measure, list) {
> > bool rc;
> >
> > - rc = ima_match_rules(entry, inode, func, mask);
> > + rc = ima_match_rules(entry, file, func, mask);
> > if (rc)
> > return entry->action;
> > }
> > @@ -222,6 +233,7 @@ enum {
> > Opt_measure = 1, Opt_dont_measure,
> > Opt_obj_user, Opt_obj_role, Opt_obj_type,
> > Opt_subj_user, Opt_subj_role, Opt_subj_type,
> > + Opt_fowner_user, Opt_fowner_role, Opt_fowner_type,
> > Opt_func, Opt_mask, Opt_fsmagic, Opt_uid
> > };
> >
> > @@ -234,6 +246,9 @@ static match_table_t policy_tokens = {
> > {Opt_subj_user, "subj_user=%s"},
> > {Opt_subj_role, "subj_role=%s"},
> > {Opt_subj_type, "subj_type=%s"},
> > + {Opt_fowner_user, "fowner_user=%s"},
> > + {Opt_fowner_role, "fowner_role=%s"},
> > + {Opt_fowner_type, "fowner_type=%s"},
> > {Opt_func, "func=%s"},
> > {Opt_mask, "mask=%s"},
> > {Opt_fsmagic, "fsmagic=%s"},
> > @@ -407,6 +422,24 @@ static int ima_parse_rule(char *rule, struct ima_measure_rule_entry *entry)
> > LSM_SUBJ_TYPE,
> > AUDIT_SUBJ_TYPE);
> > break;
> > + case Opt_fowner_user:
> > + ima_log_string(ab, "fowner_user", args[0].from);
> > + result = ima_lsm_rule_init(entry, args[0].from,
> > + LSM_FOWNER_USER,
> > + AUDIT_SUBJ_USER);
> > + break;
> > + case Opt_fowner_role:
> > + ima_log_string(ab, "fowner_role", args[0].from);
> > + result = ima_lsm_rule_init(entry, args[0].from,
> > + LSM_FOWNER_ROLE,
> > + AUDIT_SUBJ_ROLE);
> > + break;
> > + case Opt_fowner_type:
> > + ima_log_string(ab, "fowner_type", args[0].from);
> > + result = ima_lsm_rule_init(entry, args[0].from,
> > + LSM_FOWNER_TYPE,
> > + AUDIT_SUBJ_TYPE);
> > + break;
> > case Opt_err:
> > ima_log_string(ab, "UNKNOWN", p);
> > result = -EINVAL;
>
>
>

2011-04-28 17:37:37

by Casey Schaufler

[permalink] [raw]
Subject: Re: [RFC][PATCH 0/7] File descriptor labeling

On 4/28/2011 5:35 AM, Roberto Sassu wrote:
> On Thursday, April 28, 2011 01:27:19 AM Tyler Hicks wrote:
>> On Wed Apr 27, 2011 at 01:19:55PM -0700, Casey Schaufler <[email protected]> wrote:
>>> On 4/27/2011 5:34 AM, Roberto Sassu wrote:
>>>> File descriptor labeling issue
>>>>
>>>> Actually SELinux and SMACK assign to file descriptors the same label of the
>>>> opening process and use it in LSM hooks security_file_permission(),
>>>> security_file_fcntl() and others to verify if the 'current' process has the
>>>> rights to perform the requested operation.
>>>>
>>>> Using the credentials of the 'current' process may be not appropriate in
>>>> case a file descriptor is opened by a kernel service (i.e. a filesystem)
>>>> and made shared among user processes. For instance, in a system with
>>>> SELinux and eCryptfs, if the process A opens an encrypted file, eCryptfs
>>>> obtains a file descriptor to access the correspondent inode in the lower
>>>> filesystem, labeled with the A's label.
>>>>
>>>> If the process B accesses the same encrypted file, it needs the 'use'
>>>> permission on the A's label other than permissions for the lower inode.
>>>> However, if B is the first accessing process, A needs the 'use' permission
>>>> on the B's label.
>>> I am having trouble understanding the argument. I will pose my
>>> question in Smack terms, as I can speak most definitively in them.
>>>
>>> A process running with a Smack label "A" creates a file, and that
>>> file gets labeled "A", as it ought. If eCryptfs is behaving correctly
>>> this ought not change. If eCryptfs in encrypting the label it needs
>>> to do so in such a way as to be able to decrypt it prior to
>>> presentation to the vfs layer, where it will be used in an access
>>> check. When the process running with a Smack label "B" comes along
>>> the vfs code will check the fetched and possibly decrypted "A"
>>> against "B" and, unless there is an explicit Smack rule in place
>>> granting "B" access to "A", fail.
>>>
>>> What is the problem? What is eCryptfs doing that prevents this
>>> from working?
>> Hi Casey - I think what Roberto is getting at is the way eCryptfs uses
>> only one lower file per eCryptfs inode. Imagine that there are 5
>> files open for ~/secret/foo at the eCryptfs layer, only 1 file is going
>> to be open in the lower filesystem and all eCryptfs file operations will
>> be multiplexed through it.
>>
>> To make things more complicated, if the eCryptfs file is opened for
>> writing, the lower file must be opened for reading and writing. This is
>> because a write operation requires eCryptfs to vfs_read() from the lower
>> filesystem, decrypt that data and then vfs_write() the new data.
>>
>> If the lower file can't be opened O_RDWR by the calling process, the
>> request is handed off to a kernel thread to open the lower file on
>> behalf of the calling process. It is definitely ugly.
>>
>> Roberto, I hope I correctly described the situation that you're trying
>> to address. Can you tell me why we can't have a 1:1 mapping of eCryptfs
>> files to lower files?
>>
>> Instead of having just one lower file attached to the eCryptfs inode, we
>> could have a list of opened files. There would be one for each eCryptfs
>> file that was opened. ecryptfs_writepage() would have to pick, in a
>> somewhat random fashion, one of the lower files to use. Of course, we
>> would still need to solve the problem of opening the lower file O_RDWR
>> when the calling process is only allowed write access (I may have just
>> answered my own question of why the 1:1 mapping technique won't solve
>> this problem).
>>
> Hi Tyler
>
> i think the 1:1 mapping isn't necessary at least from the security perspective.
> Since eCryptfs is a stacked filesystem access control is performed on
> both the upper and the lower layer.
> ECryptfs relies on the lower filesystem for the management of extended
> attributes, so this means that the security label of both the upper and
> the lower inodes is the same (however this is not the current behavior
> in SELinux, which assigns the label 'ecryptfs_t' to the upper inode).

Where does this assignment occur?

> In my view, for this reason the access control checks can be performed
> only at the upper layer, letting eCryptfs full privileges to access inodes
> in the lower filesystem.

On this point I most strongly disagree.

The behavior of a filesystem and the data that it uses to determine
that behavior is wrought with complex interactions which may include
but are not limited to caching, read-aheads, garbage collection,
and various side effects of access control. If eCryptfs needs to go
mucking about with the data used by the underlying filesystem it is
not stacking properly. A stacked filesystem has no business whatever
changing the data of the underlying filesystem.

> This solves the problem of opening the lower file in r/w mode even if only
> the read is requested, because at the upper layer the subject is the
> accessing process with its own credentials which needs the read permission
> and at the lower layer the subject is the eCryptfs kernel module with
> unlimited privileges.

Excuse my ignorance for a moment. Is eCryptfs a user mode filesystem,
or in the kernel properly? The behavior makes it sound like the former
while the interfaces you're requesting make it seem like the latter.

> The issue i described in the cover letter is related to the label assigned
> to the file descriptor obtained by eCryptfs (or another kernel service) when
> opening an inode in the lower filesystem, which actually depends on the
> first accessing process.
> This label is checked against the credentials of the 'current' process in the
> hook security_file_permission(), which is triggered by vfs calls (read, write,
> readdir) performed on both the upper and the lower inodes.
> In SELinux, a process needs the permission to 'use' a opened file descriptor.
> So, having a fixed label helps in defining the rule that must be added in the
> policy for eCryptfs to ensure it works properly.

I'm afraid to suggest this, but it looks as if you may be
able to solve your problems with some SELinux policy. I am
of course not an expert on SELinux policy, but it looks as
if not specifying an appropriate policy for the user space
component is what is in the way here.

> PS: i'm adding in CC the Apparmor's mantainer and the mailing list to have
> their opinion about the protection offered for the eCryptfs filesystem and
> other kernel services. The overall thread is available at the url:
>
> https://lkml.org/lkml/2011/4/27/201
>
> Thanks
>
> Roberto Sassu
>
>
>> Tyler
>>
>

2011-04-28 17:56:07

by Eric Paris

[permalink] [raw]
Subject: Re: [RFC][PATCH 0/7] File descriptor labeling

On Thu, Apr 28, 2011 at 1:37 PM, Casey Schaufler <[email protected]> wrote:
> On 4/28/2011 5:35 AM, Roberto Sassu wrote:
>> On Thursday, April 28, 2011 01:27:19 AM Tyler Hicks wrote:
>>> On Wed Apr 27, 2011 at 01:19:55PM -0700, Casey Schaufler <[email protected]> wrote:
>>>> On 4/27/2011 5:34 AM, Roberto Sassu wrote:

> On this point I most strongly disagree.


Casey, I'm glad you're trying to figure out what's going on here
because I certainly don't understand all the problems!

If there were some mechanism by which the 'lower' inode could be ONLY
accessibly by ecyptfs kernel internals it oculd be marked IS_PRIVATE
and skip all security checks on it. Then you only have SELinux
security checks on the upper inode. Which seems to make sense. My
problem is that this is ONLY acceptable if there is no way for
userspace to directly reference the lower struct inode.

Just a thought.

-Eric

2011-04-29 09:41:57

by Roberto Sassu

[permalink] [raw]
Subject: Re: [RFC][PATCH 0/7] File descriptor labeling

Sorry for resending, it was rejected by the mailing lists due to
the html formatting.


On Thursday, April 28, 2011 07:37:29 PM Casey Schaufler wrote:
> On 4/28/2011 5:35 AM, Roberto Sassu wrote:
> > On Thursday, April 28, 2011 01:27:19 AM Tyler Hicks wrote:
> >> On Wed Apr 27, 2011 at 01:19:55PM -0700, Casey Schaufler <[email protected]> wrote:
> >>> On 4/27/2011 5:34 AM, Roberto Sassu wrote:
> >>>> File descriptor labeling issue
> >>>>
> >>>> Actually SELinux and SMACK assign to file descriptors the same label of the
> >>>> opening process and use it in LSM hooks security_file_permission(),
> >>>> security_file_fcntl() and others to verify if the 'current' process has the
> >>>> rights to perform the requested operation.
> >>>>
> >>>> Using the credentials of the 'current' process may be not appropriate in
> >>>> case a file descriptor is opened by a kernel service (i.e. a filesystem)
> >>>> and made shared among user processes. For instance, in a system with
> >>>> SELinux and eCryptfs, if the process A opens an encrypted file, eCryptfs
> >>>> obtains a file descriptor to access the correspondent inode in the lower
> >>>> filesystem, labeled with the A's label.
> >>>>
> >>>> If the process B accesses the same encrypted file, it needs the 'use'
> >>>> permission on the A's label other than permissions for the lower inode.
> >>>> However, if B is the first accessing process, A needs the 'use' permission
> >>>> on the B's label.
> >>> I am having trouble understanding the argument. I will pose my
> >>> question in Smack terms, as I can speak most definitively in them.
> >>>
> >>> A process running with a Smack label "A" creates a file, and that
> >>> file gets labeled "A", as it ought. If eCryptfs is behaving correctly
> >>> this ought not change. If eCryptfs in encrypting the label it needs
> >>> to do so in such a way as to be able to decrypt it prior to
> >>> presentation to the vfs layer, where it will be used in an access
> >>> check. When the process running with a Smack label "B" comes along
> >>> the vfs code will check the fetched and possibly decrypted "A"
> >>> against "B" and, unless there is an explicit Smack rule in place
> >>> granting "B" access to "A", fail.
> >>>
> >>> What is the problem? What is eCryptfs doing that prevents this
> >>> from working?
> >> Hi Casey - I think what Roberto is getting at is the way eCryptfs uses
> >> only one lower file per eCryptfs inode. Imagine that there are 5
> >> files open for ~/secret/foo at the eCryptfs layer, only 1 file is going
> >> to be open in the lower filesystem and all eCryptfs file operations will
> >> be multiplexed through it.
> >>
> >> To make things more complicated, if the eCryptfs file is opened for
> >> writing, the lower file must be opened for reading and writing. This is
> >> because a write operation requires eCryptfs to vfs_read() from the lower
> >> filesystem, decrypt that data and then vfs_write() the new data.
> >>
> >> If the lower file can't be opened O_RDWR by the calling process, the
> >> request is handed off to a kernel thread to open the lower file on
> >> behalf of the calling process. It is definitely ugly.
> >>
> >> Roberto, I hope I correctly described the situation that you're trying
> >> to address. Can you tell me why we can't have a 1:1 mapping of eCryptfs
> >> files to lower files?
> >>
> >> Instead of having just one lower file attached to the eCryptfs inode, we
> >> could have a list of opened files. There would be one for each eCryptfs
> >> file that was opened. ecryptfs_writepage() would have to pick, in a
> >> somewhat random fashion, one of the lower files to use. Of course, we
> >> would still need to solve the problem of opening the lower file O_RDWR
> >> when the calling process is only allowed write access (I may have just
> >> answered my own question of why the 1:1 mapping technique won't solve
> >> this problem).
> >>
> > Hi Tyler
> >
> > i think the 1:1 mapping isn't necessary at least from the security perspective.
> > Since eCryptfs is a stacked filesystem access control is performed on
> > both the upper and the lower layer.
> > ECryptfs relies on the lower filesystem for the management of extended
> > attributes, so this means that the security label of both the upper and
> > the lower inodes is the same (however this is not the current behavior
> > in SELinux, which assigns the label 'ecryptfs_t' to the upper inode).
>
> Where does this assignment occur?
>

Hi Casey

The assignment happens at the inode's initialization time and depends on
the behavior configured for a specific filesystem.
The SELinux reference policy actually configures static labeling for inodes
in the eCryptfs filesystem while for example allows ext4 inodes to be
initialized using extended attributes.


> > In my view, for this reason the access control checks can be performed
> > only at the upper layer, letting eCryptfs full privileges to access inodes
> > in the lower filesystem.
>
> On this point I most strongly disagree.
>
> The behavior of a filesystem and the data that it uses to determine
> that behavior is wrought with complex interactions which may include
> but are not limited to caching, read-aheads, garbage collection,
> and various side effects of access control. If eCryptfs needs to go
> mucking about with the data used by the underlying filesystem it is
> not stacking properly. A stacked filesystem has no business whatever
> changing the data of the underlying filesystem.
>

Ok, probably i have to go more in deep to explain how access control is
performed on eCryptfs. I'm talking for the SELinux's case, so SELinux experts
can correct me if i'm wrong.

First, i want to use extended attributes to initialize an eCryptfs inode, so
both the upper and the lower inodes have the same security context
because eCryptfs calls the *xattr() methods of the underlying filesystem.

Then, the process A accesses the eCryptfs inode 'I'. On the upper layer access
control is performed using the credentials of the 'current' process and the
security context of I.

If the process is allowed to access the upper inode, eCryptfs opens the lower
inode, presenting its own credentials with the type 'kernel_t'. Then, SELinux
checks if 'kernel_t' can access the inode with the security context of 'I'. If so,
eCryptfs obtains a file descriptor which, in this patch set, is labeled using the
same credentials it provided at open time and binds it to the upper inode.

Suppose for a moment that A is working on the eCryptfs inode 'I' and the process
B requests the same inode. ECryptfs uses the already opened file descriptor and
B will be granted to open the requested inode if:

- B has the 'open' permission on the upper inode 'I';


and can perform read/write operations if:

- B has the 'read' and 'write' permissions on the upper inode 'I';
- B is allowed to use the file descriptor opened by eCryptfs ('kernel_t').
- B has the 'read' and 'write' permissions on the lower inode I;


What i miss to say in my previous email is that even if eCryptfs is granted
to access inodes in the lower filesystem with full privileges, user processes
still need the permission to deal with the lower inode, but the check on the
upper inode is sufficient because the security context is the same.

Roberto Sassu


> > This solves the problem of opening the lower file in r/w mode even if only
> > the read is requested, because at the upper layer the subject is the
> > accessing process with its own credentials which needs the read permission
> > and at the lower layer the subject is the eCryptfs kernel module with
> > unlimited privileges.
>
> Excuse my ignorance for a moment. Is eCryptfs a user mode filesystem,
> or in the kernel properly? The behavior makes it sound like the former
> while the interfaces you're requesting make it seem like the latter.
>
> > The issue i described in the cover letter is related to the label assigned
> > to the file descriptor obtained by eCryptfs (or another kernel service) when
> > opening an inode in the lower filesystem, which actually depends on the
> > first accessing process.
> > This label is checked against the credentials of the 'current' process in the
> > hook security_file_permission(), which is triggered by vfs calls (read, write,
> > readdir) performed on both the upper and the lower inodes.
> > In SELinux, a process needs the permission to 'use' a opened file descriptor.
> > So, having a fixed label helps in defining the rule that must be added in the
> > policy for eCryptfs to ensure it works properly.
>
> I'm afraid to suggest this, but it looks as if you may be
> able to solve your problems with some SELinux policy. I am
> of course not an expert on SELinux policy, but it looks as
> if not specifying an appropriate policy for the user space
> component is what is in the way here.
>
> > PS: i'm adding in CC the Apparmor's mantainer and the mailing list to have
> > their opinion about the protection offered for the eCryptfs filesystem and
> > other kernel services. The overall thread is available at the url:
> >
> > https://lkml.org/lkml/2011/4/27/201
> >
> > Thanks
> >
> > Roberto Sassu
> >
> >
> >> Tyler
> >>
> >
>
>

2011-04-29 15:46:28

by Casey Schaufler

[permalink] [raw]
Subject: Re: [RFC][PATCH 0/7] File descriptor labeling

On 4/29/2011 2:39 AM, Roberto Sassu wrote:
> Sorry for resending, it was rejected by the mailing lists due to
> the html formatting.
>
>
> On Thursday, April 28, 2011 07:37:29 PM Casey Schaufler wrote:
>> On 4/28/2011 5:35 AM, Roberto Sassu wrote:
>>> On Thursday, April 28, 2011 01:27:19 AM Tyler Hicks wrote:
>>>> On Wed Apr 27, 2011 at 01:19:55PM -0700, Casey Schaufler <[email protected]> wrote:
>>>>> On 4/27/2011 5:34 AM, Roberto Sassu wrote:
>>>>>> File descriptor labeling issue
>>>>>>
>>>>>> Actually SELinux and SMACK assign to file descriptors the same label of the
>>>>>> opening process and use it in LSM hooks security_file_permission(),
>>>>>> security_file_fcntl() and others to verify if the 'current' process has the
>>>>>> rights to perform the requested operation.
>>>>>>
>>>>>> Using the credentials of the 'current' process may be not appropriate in
>>>>>> case a file descriptor is opened by a kernel service (i.e. a filesystem)
>>>>>> and made shared among user processes. For instance, in a system with
>>>>>> SELinux and eCryptfs, if the process A opens an encrypted file, eCryptfs
>>>>>> obtains a file descriptor to access the correspondent inode in the lower
>>>>>> filesystem, labeled with the A's label.
>>>>>>
>>>>>> If the process B accesses the same encrypted file, it needs the 'use'
>>>>>> permission on the A's label other than permissions for the lower inode.
>>>>>> However, if B is the first accessing process, A needs the 'use' permission
>>>>>> on the B's label.
>>>>> I am having trouble understanding the argument. I will pose my
>>>>> question in Smack terms, as I can speak most definitively in them.
>>>>>
>>>>> A process running with a Smack label "A" creates a file, and that
>>>>> file gets labeled "A", as it ought. If eCryptfs is behaving correctly
>>>>> this ought not change. If eCryptfs in encrypting the label it needs
>>>>> to do so in such a way as to be able to decrypt it prior to
>>>>> presentation to the vfs layer, where it will be used in an access
>>>>> check. When the process running with a Smack label "B" comes along
>>>>> the vfs code will check the fetched and possibly decrypted "A"
>>>>> against "B" and, unless there is an explicit Smack rule in place
>>>>> granting "B" access to "A", fail.
>>>>>
>>>>> What is the problem? What is eCryptfs doing that prevents this
>>>>> from working?
>>>> Hi Casey - I think what Roberto is getting at is the way eCryptfs uses
>>>> only one lower file per eCryptfs inode. Imagine that there are 5
>>>> files open for ~/secret/foo at the eCryptfs layer, only 1 file is going
>>>> to be open in the lower filesystem and all eCryptfs file operations will
>>>> be multiplexed through it.
>>>>
>>>> To make things more complicated, if the eCryptfs file is opened for
>>>> writing, the lower file must be opened for reading and writing. This is
>>>> because a write operation requires eCryptfs to vfs_read() from the lower
>>>> filesystem, decrypt that data and then vfs_write() the new data.
>>>>
>>>> If the lower file can't be opened O_RDWR by the calling process, the
>>>> request is handed off to a kernel thread to open the lower file on
>>>> behalf of the calling process. It is definitely ugly.
>>>>
>>>> Roberto, I hope I correctly described the situation that you're trying
>>>> to address. Can you tell me why we can't have a 1:1 mapping of eCryptfs
>>>> files to lower files?
>>>>
>>>> Instead of having just one lower file attached to the eCryptfs inode, we
>>>> could have a list of opened files. There would be one for each eCryptfs
>>>> file that was opened. ecryptfs_writepage() would have to pick, in a
>>>> somewhat random fashion, one of the lower files to use. Of course, we
>>>> would still need to solve the problem of opening the lower file O_RDWR
>>>> when the calling process is only allowed write access (I may have just
>>>> answered my own question of why the 1:1 mapping technique won't solve
>>>> this problem).
>>>>
>>> Hi Tyler
>>>
>>> i think the 1:1 mapping isn't necessary at least from the security perspective.
>>> Since eCryptfs is a stacked filesystem access control is performed on
>>> both the upper and the lower layer.
>>> ECryptfs relies on the lower filesystem for the management of extended
>>> attributes, so this means that the security label of both the upper and
>>> the lower inodes is the same (however this is not the current behavior
>>> in SELinux, which assigns the label 'ecryptfs_t' to the upper inode).
>> Where does this assignment occur?
>>
> Hi Casey
>
> The assignment happens at the inode's initialization time and depends on
> the behavior configured for a specific filesystem.
> The SELinux reference policy actually configures static labeling for inodes
> in the eCryptfs filesystem while for example allows ext4 inodes to be
> initialized using extended attributes.

So how about changing eCryptfs (Goodness, but I hate camelCase) to
properly support extended attributes? That would seem a better
approach than mucking up the entire LSM. You still have to deal
with the SELinux policy, but I don't see you getting away without
doing something with that in any case.

>>> In my view, for this reason the access control checks can be performed
>>> only at the upper layer, letting eCryptfs full privileges to access inodes
>>> in the lower filesystem.
>> On this point I most strongly disagree.
>>
>> The behavior of a filesystem and the data that it uses to determine
>> that behavior is wrought with complex interactions which may include
>> but are not limited to caching, read-aheads, garbage collection,
>> and various side effects of access control. If eCryptfs needs to go
>> mucking about with the data used by the underlying filesystem it is
>> not stacking properly. A stacked filesystem has no business whatever
>> changing the data of the underlying filesystem.
>>
> Ok, probably i have to go more in deep to explain how access control is
> performed on eCryptfs. I'm talking for the SELinux's case, so SELinux experts
> can correct me if i'm wrong.
>
> First, i want to use extended attributes to initialize an eCryptfs inode, so
> both the upper and the lower inodes have the same security context
> because eCryptfs calls the *xattr() methods of the underlying filesystem.

OK, you could create your own xattr functions that do whatever credential
mucking they need to and then call the underlying file system's version.
It is possible that the work the NFS people have been doing for xattr
support (I haven't seen much on this lately, is it still active? Anyone?)
would prove instructive.

> Then, the process A accesses the eCryptfs inode 'I'. On the upper layer access
> control is performed using the credentials of the 'current' process and the
> security context of I.
>
> If the process is allowed to access the upper inode, eCryptfs opens the lower
> inode, presenting its own credentials with the type 'kernel_t'.

So eCryptfs provides its own process context. When you say "open" do you
mean open(2) or something else? A little precision goes a long way in these
discussions.

> Then, SELinux
> checks if 'kernel_t' can access the inode with the security context of 'I'.

Why doesn't eCryptfs provide the credentials of 'I'? In the kernel you
can do what you will.

> If so,
> eCryptfs obtains a file descriptor which, in this patch set, is labeled using the
> same credentials it provided at open time and binds it to the upper inode.
>
> Suppose for a moment that A is working on the eCryptfs inode 'I' and the process
> B requests the same inode. ECryptfs uses the already opened file descriptor and
> B will be granted to open the requested inode if:
>
> - B has the 'open' permission on the upper inode 'I';
>
>
> and can perform read/write operations if:
>
> - B has the 'read' and 'write' permissions on the upper inode 'I';
> - B is allowed to use the file descriptor opened by eCryptfs ('kernel_t').
> - B has the 'read' and 'write' permissions on the lower inode I;
>
>
> What i miss to say in my previous email is that even if eCryptfs is granted
> to access inodes in the lower filesystem with full privileges, user processes
> still need the permission to deal with the lower inode, but the check on the
> upper inode is sufficient because the security context is the same.

Right, but what I don't understand is why you can't use existing
mechanisms to achieve this? NFS manages (mostly).

>> Excuse my ignorance for a moment. Is eCryptfs a user mode filesystem,
>> or in the kernel properly? The behavior makes it sound like the former
>> while the interfaces you're requesting make it seem like the latter.

I'll repeat the question because it matters.

2011-05-02 08:56:48

by Roberto Sassu

[permalink] [raw]
Subject: Re: [RFC][PATCH 0/7] File descriptor labeling

On Friday, April 29, 2011 05:46:20 PM Casey Schaufler wrote:
> On 4/29/2011 2:39 AM, Roberto Sassu wrote:
> > Sorry for resending, it was rejected by the mailing lists due to
> > the html formatting.
> >
> >
> > On Thursday, April 28, 2011 07:37:29 PM Casey Schaufler wrote:
> >> On 4/28/2011 5:35 AM, Roberto Sassu wrote:
> >>> On Thursday, April 28, 2011 01:27:19 AM Tyler Hicks wrote:
> >>>> On Wed Apr 27, 2011 at 01:19:55PM -0700, Casey Schaufler <[email protected]> wrote:
> >>>>> On 4/27/2011 5:34 AM, Roberto Sassu wrote:
> >>>>>> File descriptor labeling issue
> >>>>>>
> >>>>>> Actually SELinux and SMACK assign to file descriptors the same label of the
> >>>>>> opening process and use it in LSM hooks security_file_permission(),
> >>>>>> security_file_fcntl() and others to verify if the 'current' process has the
> >>>>>> rights to perform the requested operation.
> >>>>>>
> >>>>>> Using the credentials of the 'current' process may be not appropriate in
> >>>>>> case a file descriptor is opened by a kernel service (i.e. a filesystem)
> >>>>>> and made shared among user processes. For instance, in a system with
> >>>>>> SELinux and eCryptfs, if the process A opens an encrypted file, eCryptfs
> >>>>>> obtains a file descriptor to access the correspondent inode in the lower
> >>>>>> filesystem, labeled with the A's label.
> >>>>>>
> >>>>>> If the process B accesses the same encrypted file, it needs the 'use'
> >>>>>> permission on the A's label other than permissions for the lower inode.
> >>>>>> However, if B is the first accessing process, A needs the 'use' permission
> >>>>>> on the B's label.
> >>>>> I am having trouble understanding the argument. I will pose my
> >>>>> question in Smack terms, as I can speak most definitively in them.
> >>>>>
> >>>>> A process running with a Smack label "A" creates a file, and that
> >>>>> file gets labeled "A", as it ought. If eCryptfs is behaving correctly
> >>>>> this ought not change. If eCryptfs in encrypting the label it needs
> >>>>> to do so in such a way as to be able to decrypt it prior to
> >>>>> presentation to the vfs layer, where it will be used in an access
> >>>>> check. When the process running with a Smack label "B" comes along
> >>>>> the vfs code will check the fetched and possibly decrypted "A"
> >>>>> against "B" and, unless there is an explicit Smack rule in place
> >>>>> granting "B" access to "A", fail.
> >>>>>
> >>>>> What is the problem? What is eCryptfs doing that prevents this
> >>>>> from working?
> >>>> Hi Casey - I think what Roberto is getting at is the way eCryptfs uses
> >>>> only one lower file per eCryptfs inode. Imagine that there are 5
> >>>> files open for ~/secret/foo at the eCryptfs layer, only 1 file is going
> >>>> to be open in the lower filesystem and all eCryptfs file operations will
> >>>> be multiplexed through it.
> >>>>
> >>>> To make things more complicated, if the eCryptfs file is opened for
> >>>> writing, the lower file must be opened for reading and writing. This is
> >>>> because a write operation requires eCryptfs to vfs_read() from the lower
> >>>> filesystem, decrypt that data and then vfs_write() the new data.
> >>>>
> >>>> If the lower file can't be opened O_RDWR by the calling process, the
> >>>> request is handed off to a kernel thread to open the lower file on
> >>>> behalf of the calling process. It is definitely ugly.
> >>>>
> >>>> Roberto, I hope I correctly described the situation that you're trying
> >>>> to address. Can you tell me why we can't have a 1:1 mapping of eCryptfs
> >>>> files to lower files?
> >>>>
> >>>> Instead of having just one lower file attached to the eCryptfs inode, we
> >>>> could have a list of opened files. There would be one for each eCryptfs
> >>>> file that was opened. ecryptfs_writepage() would have to pick, in a
> >>>> somewhat random fashion, one of the lower files to use. Of course, we
> >>>> would still need to solve the problem of opening the lower file O_RDWR
> >>>> when the calling process is only allowed write access (I may have just
> >>>> answered my own question of why the 1:1 mapping technique won't solve
> >>>> this problem).
> >>>>
> >>> Hi Tyler
> >>>
> >>> i think the 1:1 mapping isn't necessary at least from the security perspective.
> >>> Since eCryptfs is a stacked filesystem access control is performed on
> >>> both the upper and the lower layer.
> >>> ECryptfs relies on the lower filesystem for the management of extended
> >>> attributes, so this means that the security label of both the upper and
> >>> the lower inodes is the same (however this is not the current behavior
> >>> in SELinux, which assigns the label 'ecryptfs_t' to the upper inode).
> >> Where does this assignment occur?
> >>
> > Hi Casey
> >
> > The assignment happens at the inode's initialization time and depends on
> > the behavior configured for a specific filesystem.
> > The SELinux reference policy actually configures static labeling for inodes
> > in the eCryptfs filesystem while for example allows ext4 inodes to be
> > initialized using extended attributes.
>
> So how about changing eCryptfs (Goodness, but I hate camelCase) to
> properly support extended attributes? That would seem a better
> approach than mucking up the entire LSM. You still have to deal
> with the SELinux policy, but I don't see you getting away without
> doing something with that in any case.
>

Hi Casey

i think having separate extended attributes in eCryptfs does not
address the issue i reported in the cover letter.
The reason is, in my opinion, the incorrect labeling of file descriptors
which causes in SELinux more permissions than those really needed
must be added in the policy. This does not depends on the inode's
security context but on the credentials provided to dentry_open().


> >>> In my view, for this reason the access control checks can be performed
> >>> only at the upper layer, letting eCryptfs full privileges to access inodes
> >>> in the lower filesystem.
> >> On this point I most strongly disagree.
> >>
> >> The behavior of a filesystem and the data that it uses to determine
> >> that behavior is wrought with complex interactions which may include
> >> but are not limited to caching, read-aheads, garbage collection,
> >> and various side effects of access control. If eCryptfs needs to go
> >> mucking about with the data used by the underlying filesystem it is
> >> not stacking properly. A stacked filesystem has no business whatever
> >> changing the data of the underlying filesystem.
> >>
> > Ok, probably i have to go more in deep to explain how access control is
> > performed on eCryptfs. I'm talking for the SELinux's case, so SELinux experts
> > can correct me if i'm wrong.
> >
> > First, i want to use extended attributes to initialize an eCryptfs inode, so
> > both the upper and the lower inodes have the same security context
> > because eCryptfs calls the *xattr() methods of the underlying filesystem.
>
> OK, you could create your own xattr functions that do whatever credential
> mucking they need to and then call the underlying file system's version.
> It is possible that the work the NFS people have been doing for xattr
> support (I haven't seen much on this lately, is it still active? Anyone?)
> would prove instructive.
>
> > Then, the process A accesses the eCryptfs inode 'I'. On the upper layer access
> > control is performed using the credentials of the 'current' process and the
> > security context of I.
> >
> > If the process is allowed to access the upper inode, eCryptfs opens the lower
> > inode, presenting its own credentials with the type 'kernel_t'.
>
> So eCryptfs provides its own process context. When you say "open" do you
> mean open(2) or something else? A little precision goes a long way in these
> discussions.
>

ECryptfs provides its own credentials when calling the function dentry_open()
to obtain a file descriptor to be shared among user processes.


> > Then, SELinux
> > checks if 'kernel_t' can access the inode with the security context of 'I'.
>
> Why doesn't eCryptfs provide the credentials of 'I'? In the kernel you
> can do what you will.
>

No, eCryptfs provides (in the following patch) the 'initial' credentials which
are obtained through the function prepare_kernel_cred() which give root
privileges. These credentials are used in the hook security_dentry_open()
to verify if eCryptfs is allowed to access the inode 'I' in the underlying
filesystem.


> > If so,
> > eCryptfs obtains a file descriptor which, in this patch set, is labeled using the
> > same credentials it provided at open time and binds it to the upper inode.
> >
> > Suppose for a moment that A is working on the eCryptfs inode 'I' and the process
> > B requests the same inode. ECryptfs uses the already opened file descriptor and
> > B will be granted to open the requested inode if:
> >
> > - B has the 'open' permission on the upper inode 'I';
> >
> >
> > and can perform read/write operations if:
> >
> > - B has the 'read' and 'write' permissions on the upper inode 'I';
> > - B is allowed to use the file descriptor opened by eCryptfs ('kernel_t').
> > - B has the 'read' and 'write' permissions on the lower inode I;
> >
> >
> > What i miss to say in my previous email is that even if eCryptfs is granted
> > to access inodes in the lower filesystem with full privileges, user processes
> > still need the permission to deal with the lower inode, but the check on the
> > upper inode is sufficient because the security context is the same.
>
> Right, but what I don't understand is why you can't use existing
> mechanisms to achieve this? NFS manages (mostly).
>

Please, can you explain better this thought?


> >> Excuse my ignorance for a moment. Is eCryptfs a user mode filesystem,
> >> or in the kernel properly? The behavior makes it sound like the former
> >> while the interfaces you're requesting make it seem like the latter.
>
> I'll repeat the question because it matters.

As already reported by Guido Trentalancia, eCryptfs is a kernel mode
filesystem, which accepts data from the VFS layer, encrypts or decrypts
them and store the result in the underlying filesystem, always passing from
the VFS interface.

Roberto Sassu


>
>

2011-05-03 22:58:53

by Casey Schaufler

[permalink] [raw]
Subject: Re: [RFC][PATCH 0/7] File descriptor labeling

On 5/2/2011 1:53 AM, Roberto Sassu wrote:
> On Friday, April 29, 2011 05:46:20 PM Casey Schaufler wrote:
>> On 4/29/2011 2:39 AM, Roberto Sassu wrote:
>>> Sorry for resending, it was rejected by the mailing lists due to
>>> the html formatting.
>>>
>>>
>>> On Thursday, April 28, 2011 07:37:29 PM Casey Schaufler wrote:
>>>> On 4/28/2011 5:35 AM, Roberto Sassu wrote:
>>>>> On Thursday, April 28, 2011 01:27:19 AM Tyler Hicks wrote:
>>>>>> On Wed Apr 27, 2011 at 01:19:55PM -0700, Casey Schaufler <[email protected]> wrote:
>>>>>>> On 4/27/2011 5:34 AM, Roberto Sassu wrote:
>>>>>>>> File descriptor labeling issue
>>>>>>>>
>>>>>>>> Actually SELinux and SMACK assign to file descriptors the same label of the
>>>>>>>> opening process and use it in LSM hooks security_file_permission(),
>>>>>>>> security_file_fcntl() and others to verify if the 'current' process has the
>>>>>>>> rights to perform the requested operation.
>>>>>>>>
>>>>>>>> Using the credentials of the 'current' process may be not appropriate in
>>>>>>>> case a file descriptor is opened by a kernel service (i.e. a filesystem)
>>>>>>>> and made shared among user processes. For instance, in a system with
>>>>>>>> SELinux and eCryptfs, if the process A opens an encrypted file, eCryptfs
>>>>>>>> obtains a file descriptor to access the correspondent inode in the lower
>>>>>>>> filesystem, labeled with the A's label.
>>>>>>>>
>>>>>>>> If the process B accesses the same encrypted file, it needs the 'use'
>>>>>>>> permission on the A's label other than permissions for the lower inode.
>>>>>>>> However, if B is the first accessing process, A needs the 'use' permission
>>>>>>>> on the B's label.
>>>>>>> I am having trouble understanding the argument. I will pose my
>>>>>>> question in Smack terms, as I can speak most definitively in them.
>>>>>>>
>>>>>>> A process running with a Smack label "A" creates a file, and that
>>>>>>> file gets labeled "A", as it ought. If eCryptfs is behaving correctly
>>>>>>> this ought not change. If eCryptfs in encrypting the label it needs
>>>>>>> to do so in such a way as to be able to decrypt it prior to
>>>>>>> presentation to the vfs layer, where it will be used in an access
>>>>>>> check. When the process running with a Smack label "B" comes along
>>>>>>> the vfs code will check the fetched and possibly decrypted "A"
>>>>>>> against "B" and, unless there is an explicit Smack rule in place
>>>>>>> granting "B" access to "A", fail.
>>>>>>>
>>>>>>> What is the problem? What is eCryptfs doing that prevents this
>>>>>>> from working?
>>>>>> Hi Casey - I think what Roberto is getting at is the way eCryptfs uses
>>>>>> only one lower file per eCryptfs inode. Imagine that there are 5
>>>>>> files open for ~/secret/foo at the eCryptfs layer, only 1 file is going
>>>>>> to be open in the lower filesystem and all eCryptfs file operations will
>>>>>> be multiplexed through it.
>>>>>>
>>>>>> To make things more complicated, if the eCryptfs file is opened for
>>>>>> writing, the lower file must be opened for reading and writing. This is
>>>>>> because a write operation requires eCryptfs to vfs_read() from the lower
>>>>>> filesystem, decrypt that data and then vfs_write() the new data.
>>>>>>
>>>>>> If the lower file can't be opened O_RDWR by the calling process, the
>>>>>> request is handed off to a kernel thread to open the lower file on
>>>>>> behalf of the calling process. It is definitely ugly.
>>>>>>
>>>>>> Roberto, I hope I correctly described the situation that you're trying
>>>>>> to address. Can you tell me why we can't have a 1:1 mapping of eCryptfs
>>>>>> files to lower files?
>>>>>>
>>>>>> Instead of having just one lower file attached to the eCryptfs inode, we
>>>>>> could have a list of opened files. There would be one for each eCryptfs
>>>>>> file that was opened. ecryptfs_writepage() would have to pick, in a
>>>>>> somewhat random fashion, one of the lower files to use. Of course, we
>>>>>> would still need to solve the problem of opening the lower file O_RDWR
>>>>>> when the calling process is only allowed write access (I may have just
>>>>>> answered my own question of why the 1:1 mapping technique won't solve
>>>>>> this problem).
>>>>>>
>>>>> Hi Tyler
>>>>>
>>>>> i think the 1:1 mapping isn't necessary at least from the security perspective.
>>>>> Since eCryptfs is a stacked filesystem access control is performed on
>>>>> both the upper and the lower layer.
>>>>> ECryptfs relies on the lower filesystem for the management of extended
>>>>> attributes, so this means that the security label of both the upper and
>>>>> the lower inodes is the same (however this is not the current behavior
>>>>> in SELinux, which assigns the label 'ecryptfs_t' to the upper inode).
>>>> Where does this assignment occur?
>>>>
>>> Hi Casey
>>>
>>> The assignment happens at the inode's initialization time and depends on
>>> the behavior configured for a specific filesystem.
>>> The SELinux reference policy actually configures static labeling for inodes
>>> in the eCryptfs filesystem while for example allows ext4 inodes to be
>>> initialized using extended attributes.
>> So how about changing eCryptfs (Goodness, but I hate camelCase) to
>> properly support extended attributes? That would seem a better
>> approach than mucking up the entire LSM. You still have to deal
>> with the SELinux policy, but I don't see you getting away without
>> doing something with that in any case.
>>
> Hi Casey
>
> i think having separate extended attributes in eCryptfs does not
> address the issue i reported in the cover letter.
> The reason is, in my opinion, the incorrect labeling of file descriptors
> which causes in SELinux more permissions than those really needed
> must be added in the policy. This does not depends on the inode's
> security context but on the credentials provided to dentry_open().

The problem is that you seem to think that eCryptfs needs
access to the file. If eCryptfs is kernel code being invoked
through the VFS you should be providing the user's credentials.

>
>>>>> In my view, for this reason the access control checks can be performed
>>>>> only at the upper layer, letting eCryptfs full privileges to access inodes
>>>>> in the lower filesystem.
>>>> On this point I most strongly disagree.
>>>>
>>>> The behavior of a filesystem and the data that it uses to determine
>>>> that behavior is wrought with complex interactions which may include
>>>> but are not limited to caching, read-aheads, garbage collection,
>>>> and various side effects of access control. If eCryptfs needs to go
>>>> mucking about with the data used by the underlying filesystem it is
>>>> not stacking properly. A stacked filesystem has no business whatever
>>>> changing the data of the underlying filesystem.
>>>>
>>> Ok, probably i have to go more in deep to explain how access control is
>>> performed on eCryptfs. I'm talking for the SELinux's case, so SELinux experts
>>> can correct me if i'm wrong.
>>>
>>> First, i want to use extended attributes to initialize an eCryptfs inode, so
>>> both the upper and the lower inodes have the same security context
>>> because eCryptfs calls the *xattr() methods of the underlying filesystem.
>> OK, you could create your own xattr functions that do whatever credential
>> mucking they need to and then call the underlying file system's version.
>> It is possible that the work the NFS people have been doing for xattr
>> support (I haven't seen much on this lately, is it still active? Anyone?)
>> would prove instructive.
>>
>>> Then, the process A accesses the eCryptfs inode 'I'. On the upper layer access
>>> control is performed using the credentials of the 'current' process and the
>>> security context of I.
>>>
>>> If the process is allowed to access the upper inode, eCryptfs opens the lower
>>> inode, presenting its own credentials with the type 'kernel_t'.
>> So eCryptfs provides its own process context. When you say "open" do you
>> mean open(2) or something else? A little precision goes a long way in these
>> discussions.
>>
> ECryptfs provides its own credentials when calling the function dentry_open()
> to obtain a file descriptor to be shared among user processes.

This is your problem. The problem is that you are not
using the credential of the user process to access the
file, you are using your own. Sharing a "file descriptor"
may seem like an optimization, but as you see it has
does not work.

>>> Then, SELinux
>>> checks if 'kernel_t' can access the inode with the security context of 'I'.
>> Why doesn't eCryptfs provide the credentials of 'I'? In the kernel you
>> can do what you will.
> No, eCryptfs provides (in the following patch) the 'initial' credentials which
> are obtained through the function prepare_kernel_cred() which give root
> privileges. These credentials are used in the hook security_dentry_open()
> to verify if eCryptfs is allowed to access the inode 'I' in the underlying
> filesystem.

But you shouldn't care if eCryptfs can access the file. You should
only care if the user process can access the file. You're kernel code,
you can do what you will.

>>> If so,
>>> eCryptfs obtains a file descriptor which, in this patch set, is labeled using the
>>> same credentials it provided at open time and binds it to the upper inode.
>>>
>>> Suppose for a moment that A is working on the eCryptfs inode 'I' and the process
>>> B requests the same inode. ECryptfs uses the already opened file descriptor and
>>> B will be granted to open the requested inode if:
>>>
>>> - B has the 'open' permission on the upper inode 'I';
>>>
>>>
>>> and can perform read/write operations if:
>>>
>>> - B has the 'read' and 'write' permissions on the upper inode 'I';
>>> - B is allowed to use the file descriptor opened by eCryptfs ('kernel_t').
>>> - B has the 'read' and 'write' permissions on the lower inode I;
>>>
>>>
>>> What i miss to say in my previous email is that even if eCryptfs is granted
>>> to access inodes in the lower filesystem with full privileges, user processes
>>> still need the permission to deal with the lower inode, but the check on the
>>> upper inode is sufficient because the security context is the same.
>> Right, but what I don't understand is why you can't use existing
>> mechanisms to achieve this? NFS manages (mostly).
>>
> Please, can you explain better this thought?

NFS provides a network "view" of an ext4 (or any type for that matter)
filesystem. NFS does not support extended attributes, but there has been
ongoing work on that. It seems that if NFS does not require the kind
of changes you are requesting you shouldn't either.

>>>> Excuse my ignorance for a moment. Is eCryptfs a user mode filesystem,
>>>> or in the kernel properly? The behavior makes it sound like the former
>>>> while the interfaces you're requesting make it seem like the latter.
>> I'll repeat the question because it matters.
> As already reported by Guido Trentalancia, eCryptfs is a kernel mode
> filesystem, which accepts data from the VFS layer, encrypts or decrypts
> them and store the result in the underlying filesystem, always passing from
> the VFS interface.
>
> Roberto Sassu
>
>
>>
> --
> This message was distributed to subscribers of the selinux mailing list.
> If you no longer wish to subscribe, send mail to [email protected] with
> the words "unsubscribe selinux" without quotes as the message.
>
>

2011-05-03 23:58:19

by John Johansen

[permalink] [raw]
Subject: Re: [RFC][PATCH 0/7] File descriptor labeling

On 05/03/2011 03:58 PM, Casey Schaufler wrote:
> On 5/2/2011 1:53 AM, Roberto Sassu wrote:
>> On Friday, April 29, 2011 05:46:20 PM Casey Schaufler wrote:
>>> On 4/29/2011 2:39 AM, Roberto Sassu wrote:
>>>> Sorry for resending, it was rejected by the mailing lists due to
>>>> the html formatting.
>>>>
>>>>
>>>> On Thursday, April 28, 2011 07:37:29 PM Casey Schaufler wrote:
>>>>> On 4/28/2011 5:35 AM, Roberto Sassu wrote:
>>>>>> On Thursday, April 28, 2011 01:27:19 AM Tyler Hicks wrote:
>>>>>>> On Wed Apr 27, 2011 at 01:19:55PM -0700, Casey Schaufler <[email protected]> wrote:
>>>>>>>> On 4/27/2011 5:34 AM, Roberto Sassu wrote:
>>>>>>>>> File descriptor labeling issue
>>>>>>>>>
>>>>>>>>> Actually SELinux and SMACK assign to file descriptors the same label of the
>>>>>>>>> opening process and use it in LSM hooks security_file_permission(),
>>>>>>>>> security_file_fcntl() and others to verify if the 'current' process has the
>>>>>>>>> rights to perform the requested operation.
>>>>>>>>>
>>>>>>>>> Using the credentials of the 'current' process may be not appropriate in
>>>>>>>>> case a file descriptor is opened by a kernel service (i.e. a filesystem)
>>>>>>>>> and made shared among user processes. For instance, in a system with
>>>>>>>>> SELinux and eCryptfs, if the process A opens an encrypted file, eCryptfs
>>>>>>>>> obtains a file descriptor to access the correspondent inode in the lower
>>>>>>>>> filesystem, labeled with the A's label.
>>>>>>>>>
>>>>>>>>> If the process B accesses the same encrypted file, it needs the 'use'
>>>>>>>>> permission on the A's label other than permissions for the lower inode.
>>>>>>>>> However, if B is the first accessing process, A needs the 'use' permission
>>>>>>>>> on the B's label.
>>>>>>>> I am having trouble understanding the argument. I will pose my
>>>>>>>> question in Smack terms, as I can speak most definitively in them.
>>>>>>>>
>>>>>>>> A process running with a Smack label "A" creates a file, and that
>>>>>>>> file gets labeled "A", as it ought. If eCryptfs is behaving correctly
>>>>>>>> this ought not change. If eCryptfs in encrypting the label it needs
>>>>>>>> to do so in such a way as to be able to decrypt it prior to
>>>>>>>> presentation to the vfs layer, where it will be used in an access
>>>>>>>> check. When the process running with a Smack label "B" comes along
>>>>>>>> the vfs code will check the fetched and possibly decrypted "A"
>>>>>>>> against "B" and, unless there is an explicit Smack rule in place
>>>>>>>> granting "B" access to "A", fail.
>>>>>>>>
>>>>>>>> What is the problem? What is eCryptfs doing that prevents this
>>>>>>>> from working?
>>>>>>> Hi Casey - I think what Roberto is getting at is the way eCryptfs uses
>>>>>>> only one lower file per eCryptfs inode. Imagine that there are 5
>>>>>>> files open for ~/secret/foo at the eCryptfs layer, only 1 file is going
>>>>>>> to be open in the lower filesystem and all eCryptfs file operations will
>>>>>>> be multiplexed through it.
>>>>>>>
>>>>>>> To make things more complicated, if the eCryptfs file is opened for
>>>>>>> writing, the lower file must be opened for reading and writing. This is
>>>>>>> because a write operation requires eCryptfs to vfs_read() from the lower
>>>>>>> filesystem, decrypt that data and then vfs_write() the new data.
>>>>>>>
>>>>>>> If the lower file can't be opened O_RDWR by the calling process, the
>>>>>>> request is handed off to a kernel thread to open the lower file on
>>>>>>> behalf of the calling process. It is definitely ugly.
>>>>>>>
>>>>>>> Roberto, I hope I correctly described the situation that you're trying
>>>>>>> to address. Can you tell me why we can't have a 1:1 mapping of eCryptfs
>>>>>>> files to lower files?
>>>>>>>
>>>>>>> Instead of having just one lower file attached to the eCryptfs inode, we
>>>>>>> could have a list of opened files. There would be one for each eCryptfs
>>>>>>> file that was opened. ecryptfs_writepage() would have to pick, in a
>>>>>>> somewhat random fashion, one of the lower files to use. Of course, we
>>>>>>> would still need to solve the problem of opening the lower file O_RDWR
>>>>>>> when the calling process is only allowed write access (I may have just
>>>>>>> answered my own question of why the 1:1 mapping technique won't solve
>>>>>>> this problem).
>>>>>>>
>>>>>> Hi Tyler
>>>>>>
>>>>>> i think the 1:1 mapping isn't necessary at least from the security perspective.
>>>>>> Since eCryptfs is a stacked filesystem access control is performed on
>>>>>> both the upper and the lower layer.
>>>>>> ECryptfs relies on the lower filesystem for the management of extended
>>>>>> attributes, so this means that the security label of both the upper and
>>>>>> the lower inodes is the same (however this is not the current behavior
>>>>>> in SELinux, which assigns the label 'ecryptfs_t' to the upper inode).
>>>>> Where does this assignment occur?
>>>>>
>>>> Hi Casey
>>>>
>>>> The assignment happens at the inode's initialization time and depends on
>>>> the behavior configured for a specific filesystem.
>>>> The SELinux reference policy actually configures static labeling for inodes
>>>> in the eCryptfs filesystem while for example allows ext4 inodes to be
>>>> initialized using extended attributes.
>>> So how about changing eCryptfs (Goodness, but I hate camelCase) to
>>> properly support extended attributes? That would seem a better
>>> approach than mucking up the entire LSM. You still have to deal
>>> with the SELinux policy, but I don't see you getting away without
>>> doing something with that in any case.
>>>
>> Hi Casey
>>
>> i think having separate extended attributes in eCryptfs does not
>> address the issue i reported in the cover letter.
>> The reason is, in my opinion, the incorrect labeling of file descriptors
>> which causes in SELinux more permissions than those really needed
>> must be added in the policy. This does not depends on the inode's
>> security context but on the credentials provided to dentry_open().
>
> The problem is that you seem to think that eCryptfs needs
> access to the file. If eCryptfs is kernel code being invoked
> through the VFS you should be providing the user's credentials.
>
I have to agree with Casey, Generally looping back through the vfs should
be using the user's credentials. This doesn't even stop you opening the
lower file with a different set of permissions (eg. rw while the upper
is opened with r).

>>
>>>>>> In my view, for this reason the access control checks can be performed
>>>>>> only at the upper layer, letting eCryptfs full privileges to access inodes
>>>>>> in the lower filesystem.
>>>>> On this point I most strongly disagree.
>>>>>
>>>>> The behavior of a filesystem and the data that it uses to determine
>>>>> that behavior is wrought with complex interactions which may include
>>>>> but are not limited to caching, read-aheads, garbage collection,
>>>>> and various side effects of access control. If eCryptfs needs to go
>>>>> mucking about with the data used by the underlying filesystem it is
>>>>> not stacking properly. A stacked filesystem has no business whatever
>>>>> changing the data of the underlying filesystem.
>>>>>
>>>> Ok, probably i have to go more in deep to explain how access control is
>>>> performed on eCryptfs. I'm talking for the SELinux's case, so SELinux experts
>>>> can correct me if i'm wrong.
>>>>
>>>> First, i want to use extended attributes to initialize an eCryptfs inode, so
>>>> both the upper and the lower inodes have the same security context
>>>> because eCryptfs calls the *xattr() methods of the underlying filesystem.
>>> OK, you could create your own xattr functions that do whatever credential
>>> mucking they need to and then call the underlying file system's version.
>>> It is possible that the work the NFS people have been doing for xattr
>>> support (I haven't seen much on this lately, is it still active? Anyone?)
>>> would prove instructive.
>>>
>>>> Then, the process A accesses the eCryptfs inode 'I'. On the upper layer access
>>>> control is performed using the credentials of the 'current' process and the
>>>> security context of I.
>>>>
>>>> If the process is allowed to access the upper inode, eCryptfs opens the lower
>>>> inode, presenting its own credentials with the type 'kernel_t'.
>>> So eCryptfs provides its own process context. When you say "open" do you
>>> mean open(2) or something else? A little precision goes a long way in these
>>> discussions.
>>>
>> ECryptfs provides its own credentials when calling the function dentry_open()
>> to obtain a file descriptor to be shared among user processes.
>
> This is your problem. The problem is that you are not
> using the credential of the user process to access the
> file, you are using your own. Sharing a "file descriptor"
> may seem like an optimization, but as you see it has
> does not work.
>
and I would add may behave differently dependent on the LSM

>>>> Then, SELinux
>>>> checks if 'kernel_t' can access the inode with the security context of 'I'.
>>> Why doesn't eCryptfs provide the credentials of 'I'? In the kernel you
>>> can do what you will.
>> No, eCryptfs provides (in the following patch) the 'initial' credentials which
>> are obtained through the function prepare_kernel_cred() which give root
>> privileges. These credentials are used in the hook security_dentry_open()
>> to verify if eCryptfs is allowed to access the inode 'I' in the underlying
>> filesystem.
>
> But you shouldn't care if eCryptfs can access the file. You should
> only care if the user process can access the file. You're kernel code,
> you can do what you will.
>
Agreed, the decision should be based off whether the user can access the file.

2011-05-04 08:51:24

by Roberto Sassu

[permalink] [raw]
Subject: Re: [RFC][PATCH 0/7] File descriptor labeling

On Wednesday, May 04, 2011 01:58:00 AM John Johansen wrote:
> On 05/03/2011 03:58 PM, Casey Schaufler wrote:
> > On 5/2/2011 1:53 AM, Roberto Sassu wrote:
> >> On Friday, April 29, 2011 05:46:20 PM Casey Schaufler wrote:
> >>> On 4/29/2011 2:39 AM, Roberto Sassu wrote:
> >>>> Sorry for resending, it was rejected by the mailing lists due to
> >>>> the html formatting.
> >>>>
> >>>>
> >>>> On Thursday, April 28, 2011 07:37:29 PM Casey Schaufler wrote:
> >>>>> On 4/28/2011 5:35 AM, Roberto Sassu wrote:
> >>>>>> On Thursday, April 28, 2011 01:27:19 AM Tyler Hicks wrote:
> >>>>>>> On Wed Apr 27, 2011 at 01:19:55PM -0700, Casey Schaufler <[email protected]> wrote:
> >>>>>>>> On 4/27/2011 5:34 AM, Roberto Sassu wrote:
> >>>>>>>>> File descriptor labeling issue
> >>>>>>>>>
> >>>>>>>>> Actually SELinux and SMACK assign to file descriptors the same label of the
> >>>>>>>>> opening process and use it in LSM hooks security_file_permission(),
> >>>>>>>>> security_file_fcntl() and others to verify if the 'current' process has the
> >>>>>>>>> rights to perform the requested operation.
> >>>>>>>>>
> >>>>>>>>> Using the credentials of the 'current' process may be not appropriate in
> >>>>>>>>> case a file descriptor is opened by a kernel service (i.e. a filesystem)
> >>>>>>>>> and made shared among user processes. For instance, in a system with
> >>>>>>>>> SELinux and eCryptfs, if the process A opens an encrypted file, eCryptfs
> >>>>>>>>> obtains a file descriptor to access the correspondent inode in the lower
> >>>>>>>>> filesystem, labeled with the A's label.
> >>>>>>>>>
> >>>>>>>>> If the process B accesses the same encrypted file, it needs the 'use'
> >>>>>>>>> permission on the A's label other than permissions for the lower inode.
> >>>>>>>>> However, if B is the first accessing process, A needs the 'use' permission
> >>>>>>>>> on the B's label.
> >>>>>>>> I am having trouble understanding the argument. I will pose my
> >>>>>>>> question in Smack terms, as I can speak most definitively in them.
> >>>>>>>>
> >>>>>>>> A process running with a Smack label "A" creates a file, and that
> >>>>>>>> file gets labeled "A", as it ought. If eCryptfs is behaving correctly
> >>>>>>>> this ought not change. If eCryptfs in encrypting the label it needs
> >>>>>>>> to do so in such a way as to be able to decrypt it prior to
> >>>>>>>> presentation to the vfs layer, where it will be used in an access
> >>>>>>>> check. When the process running with a Smack label "B" comes along
> >>>>>>>> the vfs code will check the fetched and possibly decrypted "A"
> >>>>>>>> against "B" and, unless there is an explicit Smack rule in place
> >>>>>>>> granting "B" access to "A", fail.
> >>>>>>>>
> >>>>>>>> What is the problem? What is eCryptfs doing that prevents this
> >>>>>>>> from working?
> >>>>>>> Hi Casey - I think what Roberto is getting at is the way eCryptfs uses
> >>>>>>> only one lower file per eCryptfs inode. Imagine that there are 5
> >>>>>>> files open for ~/secret/foo at the eCryptfs layer, only 1 file is going
> >>>>>>> to be open in the lower filesystem and all eCryptfs file operations will
> >>>>>>> be multiplexed through it.
> >>>>>>>
> >>>>>>> To make things more complicated, if the eCryptfs file is opened for
> >>>>>>> writing, the lower file must be opened for reading and writing. This is
> >>>>>>> because a write operation requires eCryptfs to vfs_read() from the lower
> >>>>>>> filesystem, decrypt that data and then vfs_write() the new data.
> >>>>>>>
> >>>>>>> If the lower file can't be opened O_RDWR by the calling process, the
> >>>>>>> request is handed off to a kernel thread to open the lower file on
> >>>>>>> behalf of the calling process. It is definitely ugly.
> >>>>>>>
> >>>>>>> Roberto, I hope I correctly described the situation that you're trying
> >>>>>>> to address. Can you tell me why we can't have a 1:1 mapping of eCryptfs
> >>>>>>> files to lower files?
> >>>>>>>
> >>>>>>> Instead of having just one lower file attached to the eCryptfs inode, we
> >>>>>>> could have a list of opened files. There would be one for each eCryptfs
> >>>>>>> file that was opened. ecryptfs_writepage() would have to pick, in a
> >>>>>>> somewhat random fashion, one of the lower files to use. Of course, we
> >>>>>>> would still need to solve the problem of opening the lower file O_RDWR
> >>>>>>> when the calling process is only allowed write access (I may have just
> >>>>>>> answered my own question of why the 1:1 mapping technique won't solve
> >>>>>>> this problem).
> >>>>>>>
> >>>>>> Hi Tyler
> >>>>>>
> >>>>>> i think the 1:1 mapping isn't necessary at least from the security perspective.
> >>>>>> Since eCryptfs is a stacked filesystem access control is performed on
> >>>>>> both the upper and the lower layer.
> >>>>>> ECryptfs relies on the lower filesystem for the management of extended
> >>>>>> attributes, so this means that the security label of both the upper and
> >>>>>> the lower inodes is the same (however this is not the current behavior
> >>>>>> in SELinux, which assigns the label 'ecryptfs_t' to the upper inode).
> >>>>> Where does this assignment occur?
> >>>>>
> >>>> Hi Casey
> >>>>
> >>>> The assignment happens at the inode's initialization time and depends on
> >>>> the behavior configured for a specific filesystem.
> >>>> The SELinux reference policy actually configures static labeling for inodes
> >>>> in the eCryptfs filesystem while for example allows ext4 inodes to be
> >>>> initialized using extended attributes.
> >>> So how about changing eCryptfs (Goodness, but I hate camelCase) to
> >>> properly support extended attributes? That would seem a better
> >>> approach than mucking up the entire LSM. You still have to deal
> >>> with the SELinux policy, but I don't see you getting away without
> >>> doing something with that in any case.
> >>>
> >> Hi Casey
> >>
> >> i think having separate extended attributes in eCryptfs does not
> >> address the issue i reported in the cover letter.
> >> The reason is, in my opinion, the incorrect labeling of file descriptors
> >> which causes in SELinux more permissions than those really needed
> >> must be added in the policy. This does not depends on the inode's
> >> security context but on the credentials provided to dentry_open().
> >
> > The problem is that you seem to think that eCryptfs needs
> > access to the file. If eCryptfs is kernel code being invoked
> > through the VFS you should be providing the user's credentials.
> >
> I have to agree with Casey, Generally looping back through the vfs should
> be using the user's credentials. This doesn't even stop you opening the
> lower file with a different set of permissions (eg. rw while the upper
> is opened with r).
>

Hi Casey and John

my patch set does not modify this behavior: VFS calls on upper inodes
made by user processes and VFS calls (read/write) made by eCryptfs
on lower inodes still use the user's credentials.

In addition, SELinux provide a model for file descriptors. They may be
opened by another subject (which provided its own credentials) and
other processes need the 'use' permission for those file descriptors
other than permissions for related inodes.

This means that, even if eCryptfs opens lower inodes with its own
credentials, user processes still need permissions to read/write both
upper and lower inodes.

One benefit of allowing eCryptfs to provide its own credentials is that
user processes must have granted only strictly required permissions.

Roberto Sassu


> >>
> >>>>>> In my view, for this reason the access control checks can be performed
> >>>>>> only at the upper layer, letting eCryptfs full privileges to access inodes
> >>>>>> in the lower filesystem.
> >>>>> On this point I most strongly disagree.
> >>>>>
> >>>>> The behavior of a filesystem and the data that it uses to determine
> >>>>> that behavior is wrought with complex interactions which may include
> >>>>> but are not limited to caching, read-aheads, garbage collection,
> >>>>> and various side effects of access control. If eCryptfs needs to go
> >>>>> mucking about with the data used by the underlying filesystem it is
> >>>>> not stacking properly. A stacked filesystem has no business whatever
> >>>>> changing the data of the underlying filesystem.
> >>>>>
> >>>> Ok, probably i have to go more in deep to explain how access control is
> >>>> performed on eCryptfs. I'm talking for the SELinux's case, so SELinux experts
> >>>> can correct me if i'm wrong.
> >>>>
> >>>> First, i want to use extended attributes to initialize an eCryptfs inode, so
> >>>> both the upper and the lower inodes have the same security context
> >>>> because eCryptfs calls the *xattr() methods of the underlying filesystem.
> >>> OK, you could create your own xattr functions that do whatever credential
> >>> mucking they need to and then call the underlying file system's version.
> >>> It is possible that the work the NFS people have been doing for xattr
> >>> support (I haven't seen much on this lately, is it still active? Anyone?)
> >>> would prove instructive.
> >>>
> >>>> Then, the process A accesses the eCryptfs inode 'I'. On the upper layer access
> >>>> control is performed using the credentials of the 'current' process and the
> >>>> security context of I.
> >>>>
> >>>> If the process is allowed to access the upper inode, eCryptfs opens the lower
> >>>> inode, presenting its own credentials with the type 'kernel_t'.
> >>> So eCryptfs provides its own process context. When you say "open" do you
> >>> mean open(2) or something else? A little precision goes a long way in these
> >>> discussions.
> >>>
> >> ECryptfs provides its own credentials when calling the function dentry_open()
> >> to obtain a file descriptor to be shared among user processes.
> >
> > This is your problem. The problem is that you are not
> > using the credential of the user process to access the
> > file, you are using your own. Sharing a "file descriptor"
> > may seem like an optimization, but as you see it has
> > does not work.
> >
> and I would add may behave differently dependent on the LSM
>
> >>>> Then, SELinux
> >>>> checks if 'kernel_t' can access the inode with the security context of 'I'.
> >>> Why doesn't eCryptfs provide the credentials of 'I'? In the kernel you
> >>> can do what you will.
> >> No, eCryptfs provides (in the following patch) the 'initial' credentials which
> >> are obtained through the function prepare_kernel_cred() which give root
> >> privileges. These credentials are used in the hook security_dentry_open()
> >> to verify if eCryptfs is allowed to access the inode 'I' in the underlying
> >> filesystem.
> >
> > But you shouldn't care if eCryptfs can access the file. You should
> > only care if the user process can access the file. You're kernel code,
> > you can do what you will.
> >
> Agreed, the decision should be based off whether the user can access the file.
>

2011-05-04 09:23:19

by Roberto Sassu

[permalink] [raw]
Subject: Re: [RFC][PATCH 0/7] File descriptor labeling

On Wednesday, May 04, 2011 12:58:48 AM Casey Schaufler wrote:
> On 5/2/2011 1:53 AM, Roberto Sassu wrote:
> > On Friday, April 29, 2011 05:46:20 PM Casey Schaufler wrote:
> >> On 4/29/2011 2:39 AM, Roberto Sassu wrote:
> >>> Sorry for resending, it was rejected by the mailing lists due to
> >>> the html formatting.
> >>>
> >>>
> >>> On Thursday, April 28, 2011 07:37:29 PM Casey Schaufler wrote:
> >>>> On 4/28/2011 5:35 AM, Roberto Sassu wrote:
> >>>>> On Thursday, April 28, 2011 01:27:19 AM Tyler Hicks wrote:
> >>>>>> On Wed Apr 27, 2011 at 01:19:55PM -0700, Casey Schaufler <[email protected]> wrote:
> >>>>>>> On 4/27/2011 5:34 AM, Roberto Sassu wrote:
> >>>>>>>> File descriptor labeling issue
> >>>>>>>>
> >>>>>>>> Actually SELinux and SMACK assign to file descriptors the same label of the
> >>>>>>>> opening process and use it in LSM hooks security_file_permission(),
> >>>>>>>> security_file_fcntl() and others to verify if the 'current' process has the
> >>>>>>>> rights to perform the requested operation.
> >>>>>>>>
> >>>>>>>> Using the credentials of the 'current' process may be not appropriate in
> >>>>>>>> case a file descriptor is opened by a kernel service (i.e. a filesystem)
> >>>>>>>> and made shared among user processes. For instance, in a system with
> >>>>>>>> SELinux and eCryptfs, if the process A opens an encrypted file, eCryptfs
> >>>>>>>> obtains a file descriptor to access the correspondent inode in the lower
> >>>>>>>> filesystem, labeled with the A's label.
> >>>>>>>>
> >>>>>>>> If the process B accesses the same encrypted file, it needs the 'use'
> >>>>>>>> permission on the A's label other than permissions for the lower inode.
> >>>>>>>> However, if B is the first accessing process, A needs the 'use' permission
> >>>>>>>> on the B's label.
> >>>>>>> I am having trouble understanding the argument. I will pose my
> >>>>>>> question in Smack terms, as I can speak most definitively in them.
> >>>>>>>
> >>>>>>> A process running with a Smack label "A" creates a file, and that
> >>>>>>> file gets labeled "A", as it ought. If eCryptfs is behaving correctly
> >>>>>>> this ought not change. If eCryptfs in encrypting the label it needs
> >>>>>>> to do so in such a way as to be able to decrypt it prior to
> >>>>>>> presentation to the vfs layer, where it will be used in an access
> >>>>>>> check. When the process running with a Smack label "B" comes along
> >>>>>>> the vfs code will check the fetched and possibly decrypted "A"
> >>>>>>> against "B" and, unless there is an explicit Smack rule in place
> >>>>>>> granting "B" access to "A", fail.
> >>>>>>>
> >>>>>>> What is the problem? What is eCryptfs doing that prevents this
> >>>>>>> from working?
> >>>>>> Hi Casey - I think what Roberto is getting at is the way eCryptfs uses
> >>>>>> only one lower file per eCryptfs inode. Imagine that there are 5
> >>>>>> files open for ~/secret/foo at the eCryptfs layer, only 1 file is going
> >>>>>> to be open in the lower filesystem and all eCryptfs file operations will
> >>>>>> be multiplexed through it.
> >>>>>>
> >>>>>> To make things more complicated, if the eCryptfs file is opened for
> >>>>>> writing, the lower file must be opened for reading and writing. This is
> >>>>>> because a write operation requires eCryptfs to vfs_read() from the lower
> >>>>>> filesystem, decrypt that data and then vfs_write() the new data.
> >>>>>>
> >>>>>> If the lower file can't be opened O_RDWR by the calling process, the
> >>>>>> request is handed off to a kernel thread to open the lower file on
> >>>>>> behalf of the calling process. It is definitely ugly.
> >>>>>>
> >>>>>> Roberto, I hope I correctly described the situation that you're trying
> >>>>>> to address. Can you tell me why we can't have a 1:1 mapping of eCryptfs
> >>>>>> files to lower files?
> >>>>>>
> >>>>>> Instead of having just one lower file attached to the eCryptfs inode, we
> >>>>>> could have a list of opened files. There would be one for each eCryptfs
> >>>>>> file that was opened. ecryptfs_writepage() would have to pick, in a
> >>>>>> somewhat random fashion, one of the lower files to use. Of course, we
> >>>>>> would still need to solve the problem of opening the lower file O_RDWR
> >>>>>> when the calling process is only allowed write access (I may have just
> >>>>>> answered my own question of why the 1:1 mapping technique won't solve
> >>>>>> this problem).
> >>>>>>
> >>>>> Hi Tyler
> >>>>>
> >>>>> i think the 1:1 mapping isn't necessary at least from the security perspective.
> >>>>> Since eCryptfs is a stacked filesystem access control is performed on
> >>>>> both the upper and the lower layer.
> >>>>> ECryptfs relies on the lower filesystem for the management of extended
> >>>>> attributes, so this means that the security label of both the upper and
> >>>>> the lower inodes is the same (however this is not the current behavior
> >>>>> in SELinux, which assigns the label 'ecryptfs_t' to the upper inode).
> >>>> Where does this assignment occur?
> >>>>
> >>> Hi Casey
> >>>
> >>> The assignment happens at the inode's initialization time and depends on
> >>> the behavior configured for a specific filesystem.
> >>> The SELinux reference policy actually configures static labeling for inodes
> >>> in the eCryptfs filesystem while for example allows ext4 inodes to be
> >>> initialized using extended attributes.
> >> So how about changing eCryptfs (Goodness, but I hate camelCase) to
> >> properly support extended attributes? That would seem a better
> >> approach than mucking up the entire LSM. You still have to deal
> >> with the SELinux policy, but I don't see you getting away without
> >> doing something with that in any case.
> >>
> > Hi Casey
> >
> > i think having separate extended attributes in eCryptfs does not
> > address the issue i reported in the cover letter.
> > The reason is, in my opinion, the incorrect labeling of file descriptors
> > which causes in SELinux more permissions than those really needed
> > must be added in the policy. This does not depends on the inode's
> > security context but on the credentials provided to dentry_open().
>
> The problem is that you seem to think that eCryptfs needs
> access to the file. If eCryptfs is kernel code being invoked
> through the VFS you should be providing the user's credentials.
>
> >
> >>>>> In my view, for this reason the access control checks can be performed
> >>>>> only at the upper layer, letting eCryptfs full privileges to access inodes
> >>>>> in the lower filesystem.
> >>>> On this point I most strongly disagree.
> >>>>
> >>>> The behavior of a filesystem and the data that it uses to determine
> >>>> that behavior is wrought with complex interactions which may include
> >>>> but are not limited to caching, read-aheads, garbage collection,
> >>>> and various side effects of access control. If eCryptfs needs to go
> >>>> mucking about with the data used by the underlying filesystem it is
> >>>> not stacking properly. A stacked filesystem has no business whatever
> >>>> changing the data of the underlying filesystem.
> >>>>
> >>> Ok, probably i have to go more in deep to explain how access control is
> >>> performed on eCryptfs. I'm talking for the SELinux's case, so SELinux experts
> >>> can correct me if i'm wrong.
> >>>
> >>> First, i want to use extended attributes to initialize an eCryptfs inode, so
> >>> both the upper and the lower inodes have the same security context
> >>> because eCryptfs calls the *xattr() methods of the underlying filesystem.
> >> OK, you could create your own xattr functions that do whatever credential
> >> mucking they need to and then call the underlying file system's version.
> >> It is possible that the work the NFS people have been doing for xattr
> >> support (I haven't seen much on this lately, is it still active? Anyone?)
> >> would prove instructive.
> >>
> >>> Then, the process A accesses the eCryptfs inode 'I'. On the upper layer access
> >>> control is performed using the credentials of the 'current' process and the
> >>> security context of I.
> >>>
> >>> If the process is allowed to access the upper inode, eCryptfs opens the lower
> >>> inode, presenting its own credentials with the type 'kernel_t'.
> >> So eCryptfs provides its own process context. When you say "open" do you
> >> mean open(2) or something else? A little precision goes a long way in these
> >> discussions.
> >>
> > ECryptfs provides its own credentials when calling the function dentry_open()
> > to obtain a file descriptor to be shared among user processes.
>
> This is your problem. The problem is that you are not
> using the credential of the user process to access the
> file, you are using your own. Sharing a "file descriptor"
> may seem like an optimization, but as you see it has
> does not work.
>
> >>> Then, SELinux
> >>> checks if 'kernel_t' can access the inode with the security context of 'I'.
> >> Why doesn't eCryptfs provide the credentials of 'I'? In the kernel you
> >> can do what you will.
> > No, eCryptfs provides (in the following patch) the 'initial' credentials which
> > are obtained through the function prepare_kernel_cred() which give root
> > privileges. These credentials are used in the hook security_dentry_open()
> > to verify if eCryptfs is allowed to access the inode 'I' in the underlying
> > filesystem.
>
> But you shouldn't care if eCryptfs can access the file. You should
> only care if the user process can access the file. You're kernel code,
> you can do what you will.
>
> >>> If so,
> >>> eCryptfs obtains a file descriptor which, in this patch set, is labeled using the
> >>> same credentials it provided at open time and binds it to the upper inode.
> >>>
> >>> Suppose for a moment that A is working on the eCryptfs inode 'I' and the process
> >>> B requests the same inode. ECryptfs uses the already opened file descriptor and
> >>> B will be granted to open the requested inode if:
> >>>
> >>> - B has the 'open' permission on the upper inode 'I';
> >>>
> >>>
> >>> and can perform read/write operations if:
> >>>
> >>> - B has the 'read' and 'write' permissions on the upper inode 'I';
> >>> - B is allowed to use the file descriptor opened by eCryptfs ('kernel_t').
> >>> - B has the 'read' and 'write' permissions on the lower inode I;
> >>>
> >>>
> >>> What i miss to say in my previous email is that even if eCryptfs is granted
> >>> to access inodes in the lower filesystem with full privileges, user processes
> >>> still need the permission to deal with the lower inode, but the check on the
> >>> upper inode is sufficient because the security context is the same.
> >> Right, but what I don't understand is why you can't use existing
> >> mechanisms to achieve this? NFS manages (mostly).
> >>
> > Please, can you explain better this thought?
>
> NFS provides a network "view" of an ext4 (or any type for that matter)
> filesystem. NFS does not support extended attributes, but there has been
> ongoing work on that. It seems that if NFS does not require the kind
> of changes you are requesting you shouldn't either.
>

I'm not familiar with NFS, so i cannot answer about this scenario.
I'm adding in CC also the NFS mantainers and the mailing list so
that they can express their point of view. The whole thread is
available at:

https://lkml.org/lkml/2011/4/27/201

Roberto Sassu


> >>>> Excuse my ignorance for a moment. Is eCryptfs a user mode filesystem,
> >>>> or in the kernel properly? The behavior makes it sound like the former
> >>>> while the interfaces you're requesting make it seem like the latter.
> >> I'll repeat the question because it matters.
> > As already reported by Guido Trentalancia, eCryptfs is a kernel mode
> > filesystem, which accepts data from the VFS layer, encrypts or decrypts
> > them and store the result in the underlying filesystem, always passing from
> > the VFS interface.
> >
> > Roberto Sassu
> >
> >
> >>
> > --
> > This message was distributed to subscribers of the selinux mailing list.
> > If you no longer wish to subscribe, send mail to [email protected] with
> > the words "unsubscribe selinux" without quotes as the message.
> >
> >
>
>

2011-05-04 17:34:55

by Casey Schaufler

[permalink] [raw]
Subject: Re: [RFC][PATCH 0/7] File descriptor labeling

On 5/4/2011 1:47 AM, Roberto Sassu wrote:
> On Wednesday, May 04, 2011 01:58:00 AM John Johansen wrote:
>> ....
>> I have to agree with Casey, Generally looping back through the vfs should
>> be using the user's credentials. This doesn't even stop you opening the
>> lower file with a different set of permissions (eg. rw while the upper
>> is opened with r).
> Hi Casey and John
>
> my patch set does not modify this behavior: VFS calls on upper inodes
> made by user processes and VFS calls (read/write) made by eCryptfs
> on lower inodes still use the user's credentials.
>
> In addition, SELinux provide a model for file descriptors. They may be
> opened by another subject (which provided its own credentials) and
> other processes need the 'use' permission for those file descriptors
> other than permissions for related inodes.
>
> This means that, even if eCryptfs opens lower inodes with its own
> credentials, user processes still need permissions to read/write both
> upper and lower inodes.
>
> One benefit of allowing eCryptfs to provide its own credentials is that
> user processes must have granted only strictly required permissions.
>
> Roberto Sassu

My point is that you should be able to achieve all of what you
say you want to do without introducing the LSM changes you are
proposing.

2011-05-04 17:42:41

by Casey Schaufler

[permalink] [raw]
Subject: Re: [RFC][PATCH 0/7] File descriptor labeling

On 5/4/2011 2:19 AM, Roberto Sassu wrote:
> On Wednesday, May 04, 2011 12:58:48 AM Casey Schaufler wrote:
>> On 5/2/2011 1:53 AM, Roberto Sassu wrote:
>> ...
>>>> Right, but what I don't understand is why you can't use existing
>>>> mechanisms to achieve this? NFS manages (mostly).
>>> Please, can you explain better this thought?
>> NFS provides a network "view" of an ext4 (or any type for that matter)
>> filesystem. NFS does not support extended attributes, but there has been
>> ongoing work on that. It seems that if NFS does not require the kind
>> of changes you are requesting you shouldn't either.
> I'm not familiar with NFS, so i cannot answer about this scenario.

NFS provides the historical and prototypical example of a layered
filesystem. If you are implementing a layered filesystem and have
not researched NFS you are missing out on the bulk of the experience
and wisdom on the subject.

> I'm adding in CC also the NFS mantainers and the mailing list so
> that they can express their point of view. The whole thread is
> available at:
>
> https://lkml.org/lkml/2011/4/27/201
>
> Roberto Sassu