From: Joel Granados via B4 Relay <devnull+j.granados.samsung.com@kernel.org>
To: smatch@vger.kernel.org, linux-sparse@vger.kernel.org
Cc: Joel Granados <j.granados@samsung.com>
Subject: [PATCH] sentinel_ctltable: Add a check for sentinel elements in ctl_table arrays
Date: Fri, 14 Jun 2024 14:29:41 +0200 [thread overview]
Message-ID: <20240614-master-v1-1-c652f5aa15fb@samsung.com> (raw)
From: Joel Granados <j.granados@samsung.com>
Added a new check named check_sentinel_ctltable that prints a warning
when a sentinel element is detected in a ctl_table struct array.
Sentinels marking the end of a ctl_table array where completely removed
from the linux kernel in [1]. We add this warning to avoid cases where a
sentinel gets added by mistake.
[1] https://lore.kernel.org/20240604-jag-sysctl_remset-v1-0-2df7ecdba0bd@samsung.com
Signed-off-by: Joel Granados <j.granados@samsung.com>
---
Signed-off-by: Joel Granados <j.granados@samsung.com>
--
---
check_list.h | 1 +
check_sentinel_ctltable.c | 41 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 42 insertions(+)
diff --git a/check_list.h b/check_list.h
index 7115b069..a870c95f 100644
--- a/check_list.h
+++ b/check_list.h
@@ -252,6 +252,7 @@ CK(check_direct_return_instead_of_goto)
CK(check_double_fget)
CK(check_negative_error_code_type_promoted)
CK(check_uninitialized_kobj)
+CK(check_sentinel_ctltable)
/* wine specific stuff */
CK(check_wine_filehandles)
diff --git a/check_sentinel_ctltable.c b/check_sentinel_ctltable.c
new file mode 100644
index 00000000..8bdb582a
--- /dev/null
+++ b/check_sentinel_ctltable.c
@@ -0,0 +1,41 @@
+#include "smatch.h"
+
+struct non_null_ctltable_elems {
+ const char *name;
+ const int len;
+};
+
+static struct non_null_ctltable_elems non_null_elems[] = {
+ {.name = "->procname", .len = 10},
+ {.name = "->proc_handler", .len = 14},
+};
+
+static int match_ctl_table_array_sentinel(struct expression *expr)
+{
+ char * member_name = NULL;
+ if (!expr)
+ return 0;
+
+ member_name = get_member_name(expr);
+ if (!member_name)
+ return 0;
+
+ if (strncmp(member_name, "(struct ctl_table)", 18) != 0)
+ return 0;
+
+ for (int i = 0 ; i < ARRAY_SIZE(non_null_elems) ; ++i) {
+ if (strncmp(member_name + 18, non_null_elems[i].name, non_null_elems[i].len) ==0) {
+ sm_warning ("(struct ctl_table)%s cannot be NULL. Expression : %s",
+ non_null_elems[i].name, expr_to_str(expr));
+ return 0;
+ }
+ }
+
+ return 0;
+}
+
+void check_sentinel_ctltable(int id)
+{
+ add_hook(&match_ctl_table_array_sentinel, EXPR_HOOK);
+}
+
---
base-commit: ff1cc4d453ffeddf3cf3dc031c5b129eefbf3e2c
change-id: 20240614-master-db259d890db0
Best regards,
--
Joel Granados <j.granados@samsung.com>
next reply other threads:[~2024-06-14 12:30 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-14 12:29 Joel Granados via B4 Relay [this message]
2024-06-14 13:28 ` [PATCH] sentinel_ctltable: Add a check for sentinel elements in ctl_table arrays Dan Carpenter
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240614-master-v1-1-c652f5aa15fb@samsung.com \
--to=devnull+j.granados.samsung.com@kernel.org \
--cc=j.granados@samsung.com \
--cc=linux-sparse@vger.kernel.org \
--cc=smatch@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).