diff mbox series

[RFC,4/6] bpf: Introduce bpf_verify_umd_signature() kfunc

Message ID 20230425173557.724688-5-roberto.sassu@huaweicloud.com (mailing list archive)
State RFC
Delegated to: BPF
Headers show
Series KEYS: Introduce user mode key and signature parsers | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ${{ matrix.test }} on ${{ matrix.arch }} with ${{ matrix.toolchain_full }}
bpf/vmtest-bpf-next-VM_Test-2 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-3 fail Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 fail Logs for build for aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-5 fail Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-6 fail Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 fail Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-8 success Logs for set-matrix
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Guessed tree name to be net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit fail Errors and warnings before: 29 this patch: 30
netdev/cc_maintainers success CCed 15 of 15 maintainers
netdev/build_clang fail Errors and warnings before: 18 this patch: 18
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn fail Errors and warnings before: 29 this patch: 30
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 99 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Roberto Sassu April 25, 2023, 5:35 p.m. UTC
From: Roberto Sassu <roberto.sassu@huawei.com>

Introduce the bpf_verify_umd_signature() kfunc, to verify UMD-parsed
signatures. The parameters and usage are the same as for
bpf_verify_pkcs7_signature().

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
---
 kernel/trace/bpf_trace.c | 69 ++++++++++++++++++++++++++++++++--------
 1 file changed, 55 insertions(+), 14 deletions(-)

Comments

Yonghong Song April 25, 2023, 9:25 p.m. UTC | #1
On 4/25/23 10:35 AM, Roberto Sassu wrote:
> From: Roberto Sassu <roberto.sassu@huawei.com>
> 
> Introduce the bpf_verify_umd_signature() kfunc, to verify UMD-parsed
> signatures. The parameters and usage are the same as for
> bpf_verify_pkcs7_signature().
> 
> Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> ---
>   kernel/trace/bpf_trace.c | 69 ++++++++++++++++++++++++++++++++--------
>   1 file changed, 55 insertions(+), 14 deletions(-)
> 
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index e8da032bb6f..c9cae337596 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -1271,7 +1271,7 @@ __bpf_kfunc struct bpf_key *bpf_lookup_user_key(u32 serial, u64 flags)
>    * The key pointer is marked as invalid, to prevent bpf_key_put() from
>    * attempting to decrement the key reference count on that pointer. The key
>    * pointer set in such way is currently understood only by
> - * verify_pkcs7_signature().
> + * verify_pkcs7_signature() and verify_umd_signature().
>    *
>    * Set *id* to one of the values defined in include/linux/verification.h:
>    * 0 for the primary keyring (immutable keyring of system keys);
> @@ -1317,6 +1317,27 @@ __bpf_kfunc void bpf_key_put(struct bpf_key *bkey)
>   }
>   
>   #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
> +static int validate_key(struct bpf_key *trusted_keyring)
> +{
> +	int ret = 0;
> +
> +	if (trusted_keyring->has_ref) {
> +		/*
> +		 * Do the permission check deferred in bpf_lookup_user_key().
> +		 * See bpf_lookup_user_key() for more details.
> +		 *
> +		 * A call to key_task_permission() here would be redundant, as
> +		 * it is already done by keyring_search() called by
> +		 * find_asymmetric_key().
> +		 */
> +		ret = key_validate(trusted_keyring->key);
> +		if (ret < 0)
> +			return ret;

The above
	if (ret < 0)
		return ret;
can be removed.

> +	}
> +
> +	return ret;
> +}
> +
>   /**
>    * bpf_verify_pkcs7_signature - verify a PKCS#7 signature
>    * @data_ptr: data to verify
> @@ -1334,19 +1355,9 @@ __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,
>   {
>   	int ret;
>   
> -	if (trusted_keyring->has_ref) {
> -		/*
> -		 * Do the permission check deferred in bpf_lookup_user_key().
> -		 * See bpf_lookup_user_key() for more details.
> -		 *
> -		 * A call to key_task_permission() here would be redundant, as
> -		 * it is already done by keyring_search() called by
> -		 * find_asymmetric_key().
> -		 */
> -		ret = key_validate(trusted_keyring->key);
> -		if (ret < 0)
> -			return ret;
> -	}
> +	ret = validate_key(trusted_keyring);
> +	if (ret < 0)
> +		return ret;
>   
>   	return verify_pkcs7_signature(data_ptr->data,
>   				      bpf_dynptr_get_size(data_ptr),
> @@ -1356,6 +1367,35 @@ __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,
>   				      VERIFYING_UNSPECIFIED_SIGNATURE, NULL,
>   				      NULL);
>   }
> +
> +/**
> + * bpf_verify_umd_signature - Verify a UMD-parsed signature
> + * @data_ptr: Data to verify
> + * @sig_ptr: Signature of the data
> + * @trusted_keyring: Keyring with keys trusted for signature verification
> + *
> + * Verify the UMD-parsed signature *sig_ptr* against the supplied *data_ptr*
> + * with keys in a keyring referenced by *trusted_keyring*.
> + *
> + * Return: 0 on success, a negative value on error.
> + */
> +__bpf_kfunc int bpf_verify_umd_signature(struct bpf_dynptr_kern *data_ptr,
> +					 struct bpf_dynptr_kern *sig_ptr,
> +					 struct bpf_key *trusted_keyring)
> +{
> +	int ret;
> +
> +	ret = validate_key(trusted_keyring);
> +	if (ret < 0)
> +		return ret;
> +
> +	return verify_umd_signature(data_ptr->data,
> +				    bpf_dynptr_get_size(data_ptr),
> +				    sig_ptr->data, bpf_dynptr_get_size(sig_ptr),
> +				    trusted_keyring->key,
> +				    VERIFYING_UNSPECIFIED_SIGNATURE, NULL,
> +				    NULL);
> +}
>   #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
>   
>   __diag_pop();
> @@ -1366,6 +1406,7 @@ BTF_ID_FLAGS(func, bpf_lookup_system_key, KF_ACQUIRE | KF_RET_NULL)
>   BTF_ID_FLAGS(func, bpf_key_put, KF_RELEASE)
>   #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
>   BTF_ID_FLAGS(func, bpf_verify_pkcs7_signature, KF_SLEEPABLE)
> +BTF_ID_FLAGS(func, bpf_verify_umd_signature, KF_SLEEPABLE)
>   #endif
>   BTF_SET8_END(key_sig_kfunc_set)
>
Roberto Sassu April 26, 2023, 11:44 a.m. UTC | #2
On Tue, 2023-04-25 at 14:25 -0700, Yonghong Song wrote:
> 
> On 4/25/23 10:35 AM, Roberto Sassu wrote:
> > From: Roberto Sassu <roberto.sassu@huawei.com>
> > 
> > Introduce the bpf_verify_umd_signature() kfunc, to verify UMD-parsed
> > signatures. The parameters and usage are the same as for
> > bpf_verify_pkcs7_signature().
> > 
> > Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
> > ---
> >   kernel/trace/bpf_trace.c | 69 ++++++++++++++++++++++++++++++++--------
> >   1 file changed, 55 insertions(+), 14 deletions(-)
> > 
> > diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> > index e8da032bb6f..c9cae337596 100644
> > --- a/kernel/trace/bpf_trace.c
> > +++ b/kernel/trace/bpf_trace.c
> > @@ -1271,7 +1271,7 @@ __bpf_kfunc struct bpf_key *bpf_lookup_user_key(u32 serial, u64 flags)
> >    * The key pointer is marked as invalid, to prevent bpf_key_put() from
> >    * attempting to decrement the key reference count on that pointer. The key
> >    * pointer set in such way is currently understood only by
> > - * verify_pkcs7_signature().
> > + * verify_pkcs7_signature() and verify_umd_signature().
> >    *
> >    * Set *id* to one of the values defined in include/linux/verification.h:
> >    * 0 for the primary keyring (immutable keyring of system keys);
> > @@ -1317,6 +1317,27 @@ __bpf_kfunc void bpf_key_put(struct bpf_key *bkey)
> >   }
> >   
> >   #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
> > +static int validate_key(struct bpf_key *trusted_keyring)
> > +{
> > +	int ret = 0;
> > +
> > +	if (trusted_keyring->has_ref) {
> > +		/*
> > +		 * Do the permission check deferred in bpf_lookup_user_key().
> > +		 * See bpf_lookup_user_key() for more details.
> > +		 *
> > +		 * A call to key_task_permission() here would be redundant, as
> > +		 * it is already done by keyring_search() called by
> > +		 * find_asymmetric_key().
> > +		 */
> > +		ret = key_validate(trusted_keyring->key);
> > +		if (ret < 0)
> > +			return ret;
> 
> The above
> 	if (ret < 0)
> 		return ret;
> can be removed.

Right, thanks!

Roberto

> > +	}
> > +
> > +	return ret;
> > +}
> > +
> >   /**
> >    * bpf_verify_pkcs7_signature - verify a PKCS#7 signature
> >    * @data_ptr: data to verify
> > @@ -1334,19 +1355,9 @@ __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,
> >   {
> >   	int ret;
> >   
> > -	if (trusted_keyring->has_ref) {
> > -		/*
> > -		 * Do the permission check deferred in bpf_lookup_user_key().
> > -		 * See bpf_lookup_user_key() for more details.
> > -		 *
> > -		 * A call to key_task_permission() here would be redundant, as
> > -		 * it is already done by keyring_search() called by
> > -		 * find_asymmetric_key().
> > -		 */
> > -		ret = key_validate(trusted_keyring->key);
> > -		if (ret < 0)
> > -			return ret;
> > -	}
> > +	ret = validate_key(trusted_keyring);
> > +	if (ret < 0)
> > +		return ret;
> >   
> >   	return verify_pkcs7_signature(data_ptr->data,
> >   				      bpf_dynptr_get_size(data_ptr),
> > @@ -1356,6 +1367,35 @@ __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,
> >   				      VERIFYING_UNSPECIFIED_SIGNATURE, NULL,
> >   				      NULL);
> >   }
> > +
> > +/**
> > + * bpf_verify_umd_signature - Verify a UMD-parsed signature
> > + * @data_ptr: Data to verify
> > + * @sig_ptr: Signature of the data
> > + * @trusted_keyring: Keyring with keys trusted for signature verification
> > + *
> > + * Verify the UMD-parsed signature *sig_ptr* against the supplied *data_ptr*
> > + * with keys in a keyring referenced by *trusted_keyring*.
> > + *
> > + * Return: 0 on success, a negative value on error.
> > + */
> > +__bpf_kfunc int bpf_verify_umd_signature(struct bpf_dynptr_kern *data_ptr,
> > +					 struct bpf_dynptr_kern *sig_ptr,
> > +					 struct bpf_key *trusted_keyring)
> > +{
> > +	int ret;
> > +
> > +	ret = validate_key(trusted_keyring);
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	return verify_umd_signature(data_ptr->data,
> > +				    bpf_dynptr_get_size(data_ptr),
> > +				    sig_ptr->data, bpf_dynptr_get_size(sig_ptr),
> > +				    trusted_keyring->key,
> > +				    VERIFYING_UNSPECIFIED_SIGNATURE, NULL,
> > +				    NULL);
> > +}
> >   #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
> >   
> >   __diag_pop();
> > @@ -1366,6 +1406,7 @@ BTF_ID_FLAGS(func, bpf_lookup_system_key, KF_ACQUIRE | KF_RET_NULL)
> >   BTF_ID_FLAGS(func, bpf_key_put, KF_RELEASE)
> >   #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
> >   BTF_ID_FLAGS(func, bpf_verify_pkcs7_signature, KF_SLEEPABLE)
> > +BTF_ID_FLAGS(func, bpf_verify_umd_signature, KF_SLEEPABLE)
> >   #endif
> >   BTF_SET8_END(key_sig_kfunc_set)
> >
diff mbox series

Patch

diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index e8da032bb6f..c9cae337596 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -1271,7 +1271,7 @@  __bpf_kfunc struct bpf_key *bpf_lookup_user_key(u32 serial, u64 flags)
  * The key pointer is marked as invalid, to prevent bpf_key_put() from
  * attempting to decrement the key reference count on that pointer. The key
  * pointer set in such way is currently understood only by
- * verify_pkcs7_signature().
+ * verify_pkcs7_signature() and verify_umd_signature().
  *
  * Set *id* to one of the values defined in include/linux/verification.h:
  * 0 for the primary keyring (immutable keyring of system keys);
@@ -1317,6 +1317,27 @@  __bpf_kfunc void bpf_key_put(struct bpf_key *bkey)
 }
 
 #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
+static int validate_key(struct bpf_key *trusted_keyring)
+{
+	int ret = 0;
+
+	if (trusted_keyring->has_ref) {
+		/*
+		 * Do the permission check deferred in bpf_lookup_user_key().
+		 * See bpf_lookup_user_key() for more details.
+		 *
+		 * A call to key_task_permission() here would be redundant, as
+		 * it is already done by keyring_search() called by
+		 * find_asymmetric_key().
+		 */
+		ret = key_validate(trusted_keyring->key);
+		if (ret < 0)
+			return ret;
+	}
+
+	return ret;
+}
+
 /**
  * bpf_verify_pkcs7_signature - verify a PKCS#7 signature
  * @data_ptr: data to verify
@@ -1334,19 +1355,9 @@  __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,
 {
 	int ret;
 
-	if (trusted_keyring->has_ref) {
-		/*
-		 * Do the permission check deferred in bpf_lookup_user_key().
-		 * See bpf_lookup_user_key() for more details.
-		 *
-		 * A call to key_task_permission() here would be redundant, as
-		 * it is already done by keyring_search() called by
-		 * find_asymmetric_key().
-		 */
-		ret = key_validate(trusted_keyring->key);
-		if (ret < 0)
-			return ret;
-	}
+	ret = validate_key(trusted_keyring);
+	if (ret < 0)
+		return ret;
 
 	return verify_pkcs7_signature(data_ptr->data,
 				      bpf_dynptr_get_size(data_ptr),
@@ -1356,6 +1367,35 @@  __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,
 				      VERIFYING_UNSPECIFIED_SIGNATURE, NULL,
 				      NULL);
 }
+
+/**
+ * bpf_verify_umd_signature - Verify a UMD-parsed signature
+ * @data_ptr: Data to verify
+ * @sig_ptr: Signature of the data
+ * @trusted_keyring: Keyring with keys trusted for signature verification
+ *
+ * Verify the UMD-parsed signature *sig_ptr* against the supplied *data_ptr*
+ * with keys in a keyring referenced by *trusted_keyring*.
+ *
+ * Return: 0 on success, a negative value on error.
+ */
+__bpf_kfunc int bpf_verify_umd_signature(struct bpf_dynptr_kern *data_ptr,
+					 struct bpf_dynptr_kern *sig_ptr,
+					 struct bpf_key *trusted_keyring)
+{
+	int ret;
+
+	ret = validate_key(trusted_keyring);
+	if (ret < 0)
+		return ret;
+
+	return verify_umd_signature(data_ptr->data,
+				    bpf_dynptr_get_size(data_ptr),
+				    sig_ptr->data, bpf_dynptr_get_size(sig_ptr),
+				    trusted_keyring->key,
+				    VERIFYING_UNSPECIFIED_SIGNATURE, NULL,
+				    NULL);
+}
 #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
 
 __diag_pop();
@@ -1366,6 +1406,7 @@  BTF_ID_FLAGS(func, bpf_lookup_system_key, KF_ACQUIRE | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_key_put, KF_RELEASE)
 #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
 BTF_ID_FLAGS(func, bpf_verify_pkcs7_signature, KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_verify_umd_signature, KF_SLEEPABLE)
 #endif
 BTF_SET8_END(key_sig_kfunc_set)