2020-11-24 15:17:04

by KP Singh

[permalink] [raw]
Subject: [PATCH bpf-next v3 0/3] Implement bpf_ima_inode_hash

From: KP Singh <[email protected]>

# v2 -> v3

- Fixed an issue pointed out by Alexei, the helper should only be
exposed to sleepable hooks.
- Update the selftests to constrain the IMA policy udpate to a loopback
filesystem specifically created for the test. Also, split this out
from the LSM test. I dropped the Ack from this last patch since this
is a re-write.

KP Singh (3):
ima: Implement ima_inode_hash
bpf: Add a BPF helper for getting the IMA hash of an inode
bpf: Add a selftest for bpf_ima_inode_hash

include/linux/ima.h | 6 ++
include/uapi/linux/bpf.h | 11 +++
kernel/bpf/bpf_lsm.c | 26 ++++++
scripts/bpf_helpers_doc.py | 2 +
security/integrity/ima/ima_main.c | 78 ++++++++++++------
tools/include/uapi/linux/bpf.h | 11 +++
tools/testing/selftests/bpf/config | 4 +
tools/testing/selftests/bpf/ima_setup.sh | 80 +++++++++++++++++++
.../selftests/bpf/prog_tests/test_ima.c | 74 +++++++++++++++++
tools/testing/selftests/bpf/progs/ima.c | 28 +++++++
10 files changed, 296 insertions(+), 24 deletions(-)
create mode 100644 tools/testing/selftests/bpf/ima_setup.sh
create mode 100644 tools/testing/selftests/bpf/prog_tests/test_ima.c
create mode 100644 tools/testing/selftests/bpf/progs/ima.c

--
2.29.2.454.gaff20da3a2-goog


2020-11-24 15:17:21

by KP Singh

[permalink] [raw]
Subject: [PATCH bpf-next v3 1/3] ima: Implement ima_inode_hash

From: KP Singh <[email protected]>

This is in preparation to add a helper for BPF LSM programs to use
IMA hashes when attached to LSM hooks. There are LSM hooks like
inode_unlink which do not have a struct file * argument and cannot
use the existing ima_file_hash API.

An inode based API is, therefore, useful in LSM based detections like an
executable trying to delete itself which rely on the inode_unlink LSM
hook.

Moreover, the ima_file_hash function does nothing with the struct file
pointer apart from calling file_inode on it and converting it to an
inode.

Signed-off-by: KP Singh <[email protected]>
---
include/linux/ima.h | 6 +++
security/integrity/ima/ima_main.c | 78 +++++++++++++++++++++----------
2 files changed, 60 insertions(+), 24 deletions(-)

diff --git a/include/linux/ima.h b/include/linux/ima.h
index 8fa7bcfb2da2..7233a2751754 100644
--- a/include/linux/ima.h
+++ b/include/linux/ima.h
@@ -29,6 +29,7 @@ extern int ima_post_read_file(struct file *file, void *buf, loff_t size,
enum kernel_read_file_id id);
extern void ima_post_path_mknod(struct dentry *dentry);
extern int ima_file_hash(struct file *file, char *buf, size_t buf_size);
+extern int ima_inode_hash(struct inode *inode, char *buf, size_t buf_size);
extern void ima_kexec_cmdline(int kernel_fd, const void *buf, int size);

#ifdef CONFIG_IMA_KEXEC
@@ -115,6 +116,11 @@ static inline int ima_file_hash(struct file *file, char *buf, size_t buf_size)
return -EOPNOTSUPP;
}

+static inline int ima_inode_hash(struct inode *inode, char *buf, size_t buf_size)
+{
+ return -EOPNOTSUPP;
+}
+
static inline void ima_kexec_cmdline(int kernel_fd, const void *buf, int size) {}
#endif /* CONFIG_IMA */

diff --git a/security/integrity/ima/ima_main.c b/security/integrity/ima/ima_main.c
index 2d1af8899cab..cb2deaa188e7 100644
--- a/security/integrity/ima/ima_main.c
+++ b/security/integrity/ima/ima_main.c
@@ -501,37 +501,14 @@ int ima_file_check(struct file *file, int mask)
}
EXPORT_SYMBOL_GPL(ima_file_check);

-/**
- * ima_file_hash - return the stored measurement if a file has been hashed and
- * is in the iint cache.
- * @file: pointer to the file
- * @buf: buffer in which to store the hash
- * @buf_size: length of the buffer
- *
- * On success, return the hash algorithm (as defined in the enum hash_algo).
- * If buf is not NULL, this function also outputs the hash into buf.
- * If the hash is larger than buf_size, then only buf_size bytes will be copied.
- * It generally just makes sense to pass a buffer capable of holding the largest
- * possible hash: IMA_MAX_DIGEST_SIZE.
- * The file hash returned is based on the entire file, including the appended
- * signature.
- *
- * If IMA is disabled or if no measurement is available, return -EOPNOTSUPP.
- * If the parameters are incorrect, return -EINVAL.
- */
-int ima_file_hash(struct file *file, char *buf, size_t buf_size)
+static int __ima_inode_hash(struct inode *inode, char *buf, size_t buf_size)
{
- struct inode *inode;
struct integrity_iint_cache *iint;
int hash_algo;

- if (!file)
- return -EINVAL;
-
if (!ima_policy_flag)
return -EOPNOTSUPP;

- inode = file_inode(file);
iint = integrity_iint_find(inode);
if (!iint)
return -EOPNOTSUPP;
@@ -558,8 +535,61 @@ int ima_file_hash(struct file *file, char *buf, size_t buf_size)

return hash_algo;
}
+
+/**
+ * ima_file_hash - return the stored measurement if a file has been hashed and
+ * is in the iint cache.
+ * @file: pointer to the file
+ * @buf: buffer in which to store the hash
+ * @buf_size: length of the buffer
+ *
+ * On success, return the hash algorithm (as defined in the enum hash_algo).
+ * If buf is not NULL, this function also outputs the hash into buf.
+ * If the hash is larger than buf_size, then only buf_size bytes will be copied.
+ * It generally just makes sense to pass a buffer capable of holding the largest
+ * possible hash: IMA_MAX_DIGEST_SIZE.
+ * The file hash returned is based on the entire file, including the appended
+ * signature.
+ *
+ * If IMA is disabled or if no measurement is available, return -EOPNOTSUPP.
+ * If the parameters are incorrect, return -EINVAL.
+ */
+int ima_file_hash(struct file *file, char *buf, size_t buf_size)
+{
+ if (!file)
+ return -EINVAL;
+
+ return __ima_inode_hash(file_inode(file), buf, buf_size);
+}
EXPORT_SYMBOL_GPL(ima_file_hash);

+/**
+ * ima_inode_hash - return the stored measurement if the inode has been hashed
+ * and is in the iint cache.
+ * @inode: pointer to the inode
+ * @buf: buffer in which to store the hash
+ * @buf_size: length of the buffer
+ *
+ * On success, return the hash algorithm (as defined in the enum hash_algo).
+ * If buf is not NULL, this function also outputs the hash into buf.
+ * If the hash is larger than buf_size, then only buf_size bytes will be copied.
+ * It generally just makes sense to pass a buffer capable of holding the largest
+ * possible hash: IMA_MAX_DIGEST_SIZE.
+ * The hash returned is based on the entire contents, including the appended
+ * signature.
+ *
+ * If IMA is disabled or if no measurement is available, return -EOPNOTSUPP.
+ * If the parameters are incorrect, return -EINVAL.
+ */
+int ima_inode_hash(struct inode *inode, char *buf, size_t buf_size)
+{
+ if (!inode)
+ return -EINVAL;
+
+ return __ima_inode_hash(inode, buf, buf_size);
+}
+EXPORT_SYMBOL_GPL(ima_inode_hash);
+
/**
* ima_post_create_tmpfile - mark newly created tmpfile as new
* @file : newly created tmpfile
--
2.29.2.454.gaff20da3a2-goog

2020-11-24 23:47:47

by Yonghong Song

[permalink] [raw]
Subject: Re: [PATCH bpf-next v3 1/3] ima: Implement ima_inode_hash



On 11/24/20 7:12 AM, KP Singh wrote:
> From: KP Singh <[email protected]>
>
> This is in preparation to add a helper for BPF LSM programs to use
> IMA hashes when attached to LSM hooks. There are LSM hooks like
> inode_unlink which do not have a struct file * argument and cannot
> use the existing ima_file_hash API.
>
> An inode based API is, therefore, useful in LSM based detections like an
> executable trying to delete itself which rely on the inode_unlink LSM
> hook.
>
> Moreover, the ima_file_hash function does nothing with the struct file
> pointer apart from calling file_inode on it and converting it to an
> inode.
>
> Signed-off-by: KP Singh <[email protected]>

There is no change for this patch compared to previous version,
so you can carry my Ack.

Acked-by: Yonghong Song <[email protected]>

2020-11-25 12:07:38

by KP Singh

[permalink] [raw]
Subject: Re: [PATCH bpf-next v3 1/3] ima: Implement ima_inode_hash

On Tue, Nov 24, 2020 at 6:35 PM Yonghong Song <[email protected]> wrote:
>
>
>
> On 11/24/20 7:12 AM, KP Singh wrote:
> > From: KP Singh <[email protected]>
> >
> > This is in preparation to add a helper for BPF LSM programs to use
> > IMA hashes when attached to LSM hooks. There are LSM hooks like
> > inode_unlink which do not have a struct file * argument and cannot
> > use the existing ima_file_hash API.
> >
> > An inode based API is, therefore, useful in LSM based detections like an
> > executable trying to delete itself which rely on the inode_unlink LSM
> > hook.
> >
> > Moreover, the ima_file_hash function does nothing with the struct file
> > pointer apart from calling file_inode on it and converting it to an
> > inode.
> >
> > Signed-off-by: KP Singh <[email protected]>
>
> There is no change for this patch compared to previous version,
> so you can carry my Ack.
>
> Acked-by: Yonghong Song <[email protected]>

I am guessing:

* We need an Ack from Mimi/James.
* As regards to which tree, I guess bpf-next would be better since the
BPF helper and the selftest depends on it

2020-11-25 12:20:35

by Daniel Borkmann

[permalink] [raw]
Subject: Re: [PATCH bpf-next v3 1/3] ima: Implement ima_inode_hash

On 11/25/20 1:04 PM, KP Singh wrote:
> On Tue, Nov 24, 2020 at 6:35 PM Yonghong Song <[email protected]> wrote:
>> On 11/24/20 7:12 AM, KP Singh wrote:
>>> From: KP Singh <[email protected]>
>>>
>>> This is in preparation to add a helper for BPF LSM programs to use
>>> IMA hashes when attached to LSM hooks. There are LSM hooks like
>>> inode_unlink which do not have a struct file * argument and cannot
>>> use the existing ima_file_hash API.
>>>
>>> An inode based API is, therefore, useful in LSM based detections like an
>>> executable trying to delete itself which rely on the inode_unlink LSM
>>> hook.
>>>
>>> Moreover, the ima_file_hash function does nothing with the struct file
>>> pointer apart from calling file_inode on it and converting it to an
>>> inode.
>>>
>>> Signed-off-by: KP Singh <[email protected]>
>>
>> There is no change for this patch compared to previous version,
>> so you can carry my Ack.
>>
>> Acked-by: Yonghong Song <[email protected]>
>
> I am guessing:
>
> * We need an Ack from Mimi/James.

Yes.

> * As regards to which tree, I guess bpf-next would be better since the
> BPF helper and the selftest depends on it

Yep, bpf-next is my preference as otherwise we're running into unnecessary
merge conflicts.

Thanks,
Daniel

2020-11-25 12:30:10

by Mimi Zohar

[permalink] [raw]
Subject: Re: [PATCH bpf-next v3 1/3] ima: Implement ima_inode_hash

On Tue, 2020-11-24 at 15:12 +0000, KP Singh wrote:
> From: KP Singh <[email protected]>
>
> This is in preparation to add a helper for BPF LSM programs to use
> IMA hashes when attached to LSM hooks. There are LSM hooks like
> inode_unlink which do not have a struct file * argument and cannot
> use the existing ima_file_hash API.
>
> An inode based API is, therefore, useful in LSM based detections like an
> executable trying to delete itself which rely on the inode_unlink LSM
> hook.
>
> Moreover, the ima_file_hash function does nothing with the struct file
> pointer apart from calling file_inode on it and converting it to an
> inode.
>
> Signed-off-by: KP Singh <[email protected]>


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

2020-11-25 23:51:05

by patchwork-bot+netdevbpf

[permalink] [raw]
Subject: Re: [PATCH bpf-next v3 0/3] Implement bpf_ima_inode_hash

Hello:

This series was applied to bpf/bpf-next.git (refs/heads/master):

On Tue, 24 Nov 2020 15:12:07 +0000 you wrote:
> From: KP Singh <[email protected]>
>
> # v2 -> v3
>
> - Fixed an issue pointed out by Alexei, the helper should only be
> exposed to sleepable hooks.
> - Update the selftests to constrain the IMA policy udpate to a loopback
> filesystem specifically created for the test. Also, split this out
> from the LSM test. I dropped the Ack from this last patch since this
> is a re-write.
>
> [...]

Here is the summary with links:
- [bpf-next,v3,1/3] ima: Implement ima_inode_hash
https://git.kernel.org/bpf/bpf-next/c/403319be5de5
- [bpf-next,v3,2/3] bpf: Add a BPF helper for getting the IMA hash of an inode
https://git.kernel.org/bpf/bpf-next/c/27672f0d280a
- [bpf-next,v3,3/3] bpf: Add a selftest for bpf_ima_inode_hash
https://git.kernel.org/bpf/bpf-next/c/898eeb122e8a

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html