All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1] apparmor: fix use of strcpy in policy_unpack_test
@ 2023-01-27 20:12 Rae Moar
  2023-01-30 20:46 ` John Johansen
  0 siblings, 1 reply; 2+ messages in thread
From: Rae Moar @ 2023-01-27 20:12 UTC (permalink / raw)
  To: john.johansen, brendanhiggins, davidgow, dlatypov
  Cc: apparmor, skhan, kunit-dev, linux-kernel, linux-kselftest,
	Rae Moar, kernel test robot

Replace the use of strcpy() in build_aa_ext_struct() in
policy_unpack_test.c with strscpy().

strscpy() is the safer method to use to ensure the buffer does not
overflow. This was found by kernel test robot:
https://lore.kernel.org/all/202301040348.NbfVsXO0-lkp@intel.com/.

Reported-by: kernel test robot <lkp@intel.com>

Signed-off-by: Rae Moar <rmoar@google.com>
---

Note: This patch is based on the apparmor-next branch. However, the
patch should also apply cleanly to the kselftest/kunit branch.

 security/apparmor/policy_unpack_test.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/security/apparmor/policy_unpack_test.c b/security/apparmor/policy_unpack_test.c
index e1bfdab524b7..5c9bde25e56d 100644
--- a/security/apparmor/policy_unpack_test.c
+++ b/security/apparmor/policy_unpack_test.c
@@ -69,31 +69,30 @@ static struct aa_ext *build_aa_ext_struct(struct policy_unpack_fixture *puf,
 
 	*buf = AA_NAME;
 	*(buf + 1) = strlen(TEST_STRING_NAME) + 1;
-	strcpy(buf + 3, TEST_STRING_NAME);
+	strscpy(buf + 3, TEST_STRING_NAME, e->end - (void *)(buf + 3));
 
 	buf = e->start + TEST_STRING_BUF_OFFSET;
 	*buf = AA_STRING;
 	*(buf + 1) = strlen(TEST_STRING_DATA) + 1;
-	strcpy(buf + 3, TEST_STRING_DATA);
-
+	strscpy(buf + 3, TEST_STRING_DATA, e->end - (void *)(buf + 3));
 	buf = e->start + TEST_NAMED_U32_BUF_OFFSET;
 	*buf = AA_NAME;
 	*(buf + 1) = strlen(TEST_U32_NAME) + 1;
-	strcpy(buf + 3, TEST_U32_NAME);
+	strscpy(buf + 3, TEST_U32_NAME, e->end - (void *)(buf + 3));
 	*(buf + 3 + strlen(TEST_U32_NAME) + 1) = AA_U32;
 	*((u32 *)(buf + 3 + strlen(TEST_U32_NAME) + 2)) = TEST_U32_DATA;
 
 	buf = e->start + TEST_NAMED_U64_BUF_OFFSET;
 	*buf = AA_NAME;
 	*(buf + 1) = strlen(TEST_U64_NAME) + 1;
-	strcpy(buf + 3, TEST_U64_NAME);
+	strscpy(buf + 3, TEST_U64_NAME, e->end - (void *)(buf + 3));
 	*(buf + 3 + strlen(TEST_U64_NAME) + 1) = AA_U64;
 	*((u64 *)(buf + 3 + strlen(TEST_U64_NAME) + 2)) = TEST_U64_DATA;
 
 	buf = e->start + TEST_NAMED_BLOB_BUF_OFFSET;
 	*buf = AA_NAME;
 	*(buf + 1) = strlen(TEST_BLOB_NAME) + 1;
-	strcpy(buf + 3, TEST_BLOB_NAME);
+	strscpy(buf + 3, TEST_BLOB_NAME, e->end - (void *)(buf + 3));
 	*(buf + 3 + strlen(TEST_BLOB_NAME) + 1) = AA_BLOB;
 	*(buf + 3 + strlen(TEST_BLOB_NAME) + 2) = TEST_BLOB_DATA_SIZE;
 	memcpy(buf + 3 + strlen(TEST_BLOB_NAME) + 6,
@@ -102,7 +101,7 @@ static struct aa_ext *build_aa_ext_struct(struct policy_unpack_fixture *puf,
 	buf = e->start + TEST_NAMED_ARRAY_BUF_OFFSET;
 	*buf = AA_NAME;
 	*(buf + 1) = strlen(TEST_ARRAY_NAME) + 1;
-	strcpy(buf + 3, TEST_ARRAY_NAME);
+	strscpy(buf + 3, TEST_ARRAY_NAME, e->end - (void *)(buf + 3));
 	*(buf + 3 + strlen(TEST_ARRAY_NAME) + 1) = AA_ARRAY;
 	*((u16 *)(buf + 3 + strlen(TEST_ARRAY_NAME) + 2)) = TEST_ARRAY_SIZE;
 

base-commit: 1b929c02afd37871d5afb9d498426f83432e71c2
-- 
2.39.1.456.gfc5497dd1b-goog


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v1] apparmor: fix use of strcpy in policy_unpack_test
  2023-01-27 20:12 [PATCH v1] apparmor: fix use of strcpy in policy_unpack_test Rae Moar
@ 2023-01-30 20:46 ` John Johansen
  0 siblings, 0 replies; 2+ messages in thread
From: John Johansen @ 2023-01-30 20:46 UTC (permalink / raw)
  To: Rae Moar, brendanhiggins, davidgow, dlatypov
  Cc: apparmor, skhan, kunit-dev, linux-kernel, linux-kselftest,
	kernel test robot

On 1/27/23 12:12, Rae Moar wrote:
> Replace the use of strcpy() in build_aa_ext_struct() in
> policy_unpack_test.c with strscpy().
> 
> strscpy() is the safer method to use to ensure the buffer does not
> overflow. This was found by kernel test robot:
> https://lore.kernel.org/all/202301040348.NbfVsXO0-lkp@intel.com/.
> 
> Reported-by: kernel test robot <lkp@intel.com>
> 
> Signed-off-by: Rae Moar <rmoar@google.com>

lgtm

I have pulled this into apparmor-next

> ---
> 
> Note: This patch is based on the apparmor-next branch. However, the
> patch should also apply cleanly to the kselftest/kunit branch.
> 
>   security/apparmor/policy_unpack_test.c | 13 ++++++-------
>   1 file changed, 6 insertions(+), 7 deletions(-)
> 
> diff --git a/security/apparmor/policy_unpack_test.c b/security/apparmor/policy_unpack_test.c
> index e1bfdab524b7..5c9bde25e56d 100644
> --- a/security/apparmor/policy_unpack_test.c
> +++ b/security/apparmor/policy_unpack_test.c
> @@ -69,31 +69,30 @@ static struct aa_ext *build_aa_ext_struct(struct policy_unpack_fixture *puf,
>   
>   	*buf = AA_NAME;
>   	*(buf + 1) = strlen(TEST_STRING_NAME) + 1;
> -	strcpy(buf + 3, TEST_STRING_NAME);
> +	strscpy(buf + 3, TEST_STRING_NAME, e->end - (void *)(buf + 3));
>   
>   	buf = e->start + TEST_STRING_BUF_OFFSET;
>   	*buf = AA_STRING;
>   	*(buf + 1) = strlen(TEST_STRING_DATA) + 1;
> -	strcpy(buf + 3, TEST_STRING_DATA);
> -
> +	strscpy(buf + 3, TEST_STRING_DATA, e->end - (void *)(buf + 3));
>   	buf = e->start + TEST_NAMED_U32_BUF_OFFSET;
>   	*buf = AA_NAME;
>   	*(buf + 1) = strlen(TEST_U32_NAME) + 1;
> -	strcpy(buf + 3, TEST_U32_NAME);
> +	strscpy(buf + 3, TEST_U32_NAME, e->end - (void *)(buf + 3));
>   	*(buf + 3 + strlen(TEST_U32_NAME) + 1) = AA_U32;
>   	*((u32 *)(buf + 3 + strlen(TEST_U32_NAME) + 2)) = TEST_U32_DATA;
>   
>   	buf = e->start + TEST_NAMED_U64_BUF_OFFSET;
>   	*buf = AA_NAME;
>   	*(buf + 1) = strlen(TEST_U64_NAME) + 1;
> -	strcpy(buf + 3, TEST_U64_NAME);
> +	strscpy(buf + 3, TEST_U64_NAME, e->end - (void *)(buf + 3));
>   	*(buf + 3 + strlen(TEST_U64_NAME) + 1) = AA_U64;
>   	*((u64 *)(buf + 3 + strlen(TEST_U64_NAME) + 2)) = TEST_U64_DATA;
>   
>   	buf = e->start + TEST_NAMED_BLOB_BUF_OFFSET;
>   	*buf = AA_NAME;
>   	*(buf + 1) = strlen(TEST_BLOB_NAME) + 1;
> -	strcpy(buf + 3, TEST_BLOB_NAME);
> +	strscpy(buf + 3, TEST_BLOB_NAME, e->end - (void *)(buf + 3));
>   	*(buf + 3 + strlen(TEST_BLOB_NAME) + 1) = AA_BLOB;
>   	*(buf + 3 + strlen(TEST_BLOB_NAME) + 2) = TEST_BLOB_DATA_SIZE;
>   	memcpy(buf + 3 + strlen(TEST_BLOB_NAME) + 6,
> @@ -102,7 +101,7 @@ static struct aa_ext *build_aa_ext_struct(struct policy_unpack_fixture *puf,
>   	buf = e->start + TEST_NAMED_ARRAY_BUF_OFFSET;
>   	*buf = AA_NAME;
>   	*(buf + 1) = strlen(TEST_ARRAY_NAME) + 1;
> -	strcpy(buf + 3, TEST_ARRAY_NAME);
> +	strscpy(buf + 3, TEST_ARRAY_NAME, e->end - (void *)(buf + 3));
>   	*(buf + 3 + strlen(TEST_ARRAY_NAME) + 1) = AA_ARRAY;
>   	*((u16 *)(buf + 3 + strlen(TEST_ARRAY_NAME) + 2)) = TEST_ARRAY_SIZE;
>   
> 
> base-commit: 1b929c02afd37871d5afb9d498426f83432e71c2


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-01-30 20:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-27 20:12 [PATCH v1] apparmor: fix use of strcpy in policy_unpack_test Rae Moar
2023-01-30 20:46 ` John Johansen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.