diff mbox series

[v1] apparmor: fix use of strcpy in policy_unpack_test

Message ID 20230127201219.775410-1-rmoar@google.com (mailing list archive)
State Accepted
Commit b54aebd4411134b525a82d663a26b2f135ecb7e8
Headers show
Series [v1] apparmor: fix use of strcpy in policy_unpack_test | expand

Commit Message

Rae Moar Jan. 27, 2023, 8:12 p.m. UTC
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(-)


base-commit: 1b929c02afd37871d5afb9d498426f83432e71c2

Comments

John Johansen Jan. 30, 2023, 8:46 p.m. UTC | #1
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
diff mbox series

Patch

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;