smatch.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
To: smatch@vger.kernel.org, error27@gmail.com
Cc: dan.carpenter@linaro.org, darren.kenny@oracle.com,
	Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
Subject: [PATCH smatch] check_uninitialized_kobj: Add a new check
Date: Thu, 30 Nov 2023 23:57:38 -0800	[thread overview]
Message-ID: <20231201075738.3337176-1-harshit.m.mogalapalli@oracle.com> (raw)

This check complains when we reach kobject_put() or kobject_get() with
kobject->state_initialized unset.

This will eventually trigger a runtime WARN().

Signed-off-by: Harshit Mogalapalli <harshit.m.mogalapalli@oracle.com>
---
 check_list.h               |  1 +
 check_uninitialized_kobj.c | 51 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)
 create mode 100644 check_uninitialized_kobj.c

diff --git a/check_list.h b/check_list.h
index a8ef0ddf..c726c521 100644
--- a/check_list.h
+++ b/check_list.h
@@ -247,6 +247,7 @@ CK(check_uaf_netdev_priv)
 //CK(check_host_input)
 CK(check_direct_return_instead_of_goto)
 CK(check_negative_error_code_type_promoted)
+CK(check_uninitialized_kobj)
 
 /* wine specific stuff */
 CK(check_wine_filehandles)
diff --git a/check_uninitialized_kobj.c b/check_uninitialized_kobj.c
new file mode 100644
index 00000000..aff36d69
--- /dev/null
+++ b/check_uninitialized_kobj.c
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2023 Oracle.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
+ */
+
+#include "smatch.h"
+#include "smatch_slist.h"
+#include "smatch_extra.h"
+
+static int my_id;
+
+static void match_kobject_function(struct expression *expr, const char *name,
+				   struct symbol *sym, void *data)
+{
+	struct sm_state *sm, *tmp;
+
+	sm = get_sm_state(SMATCH_EXTRA, name, sym);
+	if (!sm)
+		return;
+
+	FOR_EACH_PTR(sm->possible, tmp) {
+		if (rl_max(estate_rl(tmp->state)).value == 0)
+			sm_warning("Calling kobject_put|get with state->initialized unset from line: %d",
+				   tmp->line);
+	} END_FOR_EACH_PTR(tmp);
+}
+
+void check_uninitialized_kobj(int id)
+{
+	my_id = id;
+
+	if (option_project != PROJ_KERNEL)
+		return;
+
+	add_function_param_key_hook("kobject_put", &match_kobject_function, 0,
+				    "$->state_initialized", NULL);
+	add_function_param_key_hook("kobject_get", &match_kobject_function, 0,
+				    "$->state_initialized", NULL);
+}
-- 
2.39.3


             reply	other threads:[~2023-12-01  7:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-01  7:57 Harshit Mogalapalli [this message]
2023-12-01  8:41 ` [PATCH smatch] check_uninitialized_kobj: Add a new check Dan Carpenter
2023-12-01 10:48   ` Harshit Mogalapalli

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=20231201075738.3337176-1-harshit.m.mogalapalli@oracle.com \
    --to=harshit.m.mogalapalli@oracle.com \
    --cc=dan.carpenter@linaro.org \
    --cc=darren.kenny@oracle.com \
    --cc=error27@gmail.com \
    --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).