diff mbox series

[07/12] selftests/mm: set -Wno-format-security to avoid uffd build warnings

Message ID 20230602013358.900637-8-jhubbard@nvidia.com (mailing list archive)
State New
Headers show
Series A minor flurry of selftest/mm fixes | expand

Commit Message

John Hubbard June 2, 2023, 1:33 a.m. UTC
The uffd_test_start() is perhaps a little too elaborate about how it
dispatches tests, leading to a clang warning that looks roughly like
this:

"uffd-unit-tests.c:1198:20: warning: format string is not a string literal
(potentially insecure) [-Wformat-security] ...note: treat the string as
an argument to avoid this.
    uffd_test_start(test_name);
"

However, it doesn't seem worth it to rewrite the way uffd_test_start()
works, given that these tests are already deeply unsafe to begin with.

Fix this by just disabling the compiler warning, but only for
uffd-unit-tests.

Signed-off-by: John Hubbard <jhubbard@nvidia.com>
---
 tools/testing/selftests/mm/Makefile | 2 ++
 1 file changed, 2 insertions(+)

Comments

David Hildenbrand June 2, 2023, 10:15 a.m. UTC | #1
On 02.06.23 03:33, John Hubbard wrote:
> The uffd_test_start() is perhaps a little too elaborate about how it
> dispatches tests, leading to a clang warning that looks roughly like
> this:
> 
> "uffd-unit-tests.c:1198:20: warning: format string is not a string literal
> (potentially insecure) [-Wformat-security] ...note: treat the string as
> an argument to avoid this.
>      uffd_test_start(test_name);
> "
> 
> However, it doesn't seem worth it to rewrite the way uffd_test_start()
> works, given that these tests are already deeply unsafe to begin with.
> 
> Fix this by just disabling the compiler warning, but only for
> uffd-unit-tests.
> 
> Signed-off-by: John Hubbard <jhubbard@nvidia.com>
> ---
>   tools/testing/selftests/mm/Makefile | 2 ++
>   1 file changed, 2 insertions(+)
> 
> diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
> index 23af4633f0f4..473bf1811552 100644
> --- a/tools/testing/selftests/mm/Makefile
> +++ b/tools/testing/selftests/mm/Makefile
> @@ -170,6 +170,8 @@ $(OUTPUT)/ksm_tests: LDLIBS += -lnuma
>   
>   $(OUTPUT)/migration: LDLIBS += -lnuma
>   
> +$(OUTPUT)/uffd-unit-tests: CFLAGS += -Wno-format-security
> +
>   local_config.mk local_config.h: check_config.sh
>   	/bin/sh ./check_config.sh $(CC)
>   

Maybe the following will silence the warning by removing test_name
completely:


---
  tools/testing/selftests/mm/uffd-unit-tests.c | 5 +----
  1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c b/tools/testing/selftests/mm/uffd-unit-tests.c
index 269c86768a02..15c76ce972be 100644
--- a/tools/testing/selftests/mm/uffd-unit-tests.c
+++ b/tools/testing/selftests/mm/uffd-unit-tests.c
@@ -1149,7 +1149,6 @@ int main(int argc, char *argv[])
  	uffd_test_case_t *test;
  	mem_type_t *mem_type;
  	uffd_test_args_t args;
-	char test_name[128];
  	const char *errmsg;
  	int has_uffd, opt;
  	int i, j;
@@ -1192,10 +1191,8 @@ int main(int argc, char *argv[])
  			mem_type = &mem_types[j];
  			if (!(test->mem_targets & mem_type->mem_flag))
  				continue;
-			snprintf(test_name, sizeof(test_name),
-				 "%s on %s", test->name, mem_type->name);
  
-			uffd_test_start(test_name);
+			uffd_test_start("%s on %s", test->name, mem_type->name);
  			if (!uffd_feature_supported(test)) {
  				uffd_test_skip("feature missing");
  				continue;


Still gives me

Testing register-ioctls on anon... done
Testing register-ioctls on shmem... done
Testing register-ioctls on shmem-private... done
Testing register-ioctls on hugetlb... skipped [reason: memory allocation failed]
Testing register-ioctls on hugetlb-private... skipped [reason: memory allocation failed]
...
John Hubbard June 2, 2023, 9:22 p.m. UTC | #2
On 6/2/23 03:15, David Hildenbrand wrote:
...
> Maybe the following will silence the warning by removing test_name
> completely:
> 
> 
> ---
>   tools/testing/selftests/mm/uffd-unit-tests.c | 5 +----
>   1 file changed, 1 insertion(+), 4 deletions(-)
> 
> diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c b/tools/testing/selftests/mm/uffd-unit-tests.c
> index 269c86768a02..15c76ce972be 100644
> --- a/tools/testing/selftests/mm/uffd-unit-tests.c
> +++ b/tools/testing/selftests/mm/uffd-unit-tests.c
> @@ -1149,7 +1149,6 @@ int main(int argc, char *argv[])
>       uffd_test_case_t *test;
>       mem_type_t *mem_type;
>       uffd_test_args_t args;
> -    char test_name[128];
>       const char *errmsg;
>       int has_uffd, opt;
>       int i, j;
> @@ -1192,10 +1191,8 @@ int main(int argc, char *argv[])
>               mem_type = &mem_types[j];
>               if (!(test->mem_targets & mem_type->mem_flag))
>                   continue;
> -            snprintf(test_name, sizeof(test_name),
> -                 "%s on %s", test->name, mem_type->name);
> 
> -            uffd_test_start(test_name);
> +            uffd_test_start("%s on %s", test->name, mem_type->name);
>               if (!uffd_feature_supported(test)) {
>                   uffd_test_skip("feature missing");
>                   continue;
> 

Yes, that does clean up one of the two warnings nicely. I'll do that
instead -Wno-format-security, thanks.

That still leaves another similar warning that fires for "errmsg".
However, that one is easily fixed because the associated macro turns out
to be unnecessary, because all callers pass a simple char* string,
without any format specifiers.

So just turning one macro into a C function fixes that, leaving us with
this (plus your fix from above):

diff --git a/tools/testing/selftests/mm/uffd-unit-tests.c b/tools/testing/selftests/mm/uffd-unit-tests.c
index 269c86768a02..04d91f144d1c 100644
--- a/tools/testing/selftests/mm/uffd-unit-tests.c
+++ b/tools/testing/selftests/mm/uffd-unit-tests.c
@@ -109,12 +109,11 @@ static void uffd_test_pass(void)
  		ksft_inc_fail_cnt();		\
  	} while (0)

-#define  uffd_test_skip(...)  do {		\
-		printf("skipped [reason: ");	\
-		printf(__VA_ARGS__);		\
-		printf("]\n");			\
-		ksft_inc_xskip_cnt();		\
-	} while (0)
+static void uffd_test_skip(const char *message)
+{
+	printf("skipped [reason: %s]\n", message);
+	ksft_inc_xskip_cnt();
+}

I'll send that in a v2.


thanks,
diff mbox series

Patch

diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
index 23af4633f0f4..473bf1811552 100644
--- a/tools/testing/selftests/mm/Makefile
+++ b/tools/testing/selftests/mm/Makefile
@@ -170,6 +170,8 @@  $(OUTPUT)/ksm_tests: LDLIBS += -lnuma
 
 $(OUTPUT)/migration: LDLIBS += -lnuma
 
+$(OUTPUT)/uffd-unit-tests: CFLAGS += -Wno-format-security
+
 local_config.mk local_config.h: check_config.sh
 	/bin/sh ./check_config.sh $(CC)