mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alexander Potapenko <glider@google.com>
To: dvyukov@google.com, kcc@google.com, akpm@linux-foundation.org,
	edumazet@google.com, mingo@elte.hu
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH 1/2] include/linux: provide a safe version of container_of()
Date: Fri, 23 Sep 2016 15:22:57 +0200	[thread overview]
Message-ID: <1474636978-41435-2-git-send-email-glider@google.com> (raw)
In-Reply-To: <1474636978-41435-1-git-send-email-glider@google.com>

Some code relies on "negative" (i.e. too big) pointer values being
returned by container_of() when its first argument is NULL. But doing
so breaks the compiler's assumptions that pointer arithmetic never
overflows.

container_of_safe() checks its arguments and returns NULL in the case
the member offset within the container is greater than the pointer to
the member, otherwise it returns the result of container_of().

Signed-off-by: Alexander Potapenko <glider@google.com>
---
Note that checkpatch.pl reports whitespace problems in this patch, but
the diff is consistent with the rest of the file.
---
 include/linux/kernel.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index d96a611..820b134 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -831,6 +831,21 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
 	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
 	(type *)( (char *)__mptr - offsetof(type,member) );})
 
+/**
+ * container_of_safe - safe version of container_of
+ * @ptr:	the pointer to the member.
+ * @type:	the type of the container struct this is embedded in.
+ * @member:	the name of the member within the struct.
+ *
+ * In the case the value of @ptr is smaller than the offset of @member within
+ * @type, return 0.
+ */
+#define container_of_safe(ptr, type, member) ({			\
+	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
+        (size_t)__mptr >= offsetof(type,member) ?		\
+	(type *)( (char *)__mptr - offsetof(type,member) ) : (type *)0 ;})
+
+
 /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
 # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
-- 
2.8.0.rc3.226.g39d4020

  reply	other threads:[~2016-09-23 13:24 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-23 13:22 [PATCH 0/2] Clang: avoid undefined behavior in llist iterators Alexander Potapenko
2016-09-23 13:22 ` Alexander Potapenko [this message]
2016-09-23 13:22 ` [PATCH 2/2] llist: introduce llist_entry_safe() Alexander Potapenko

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=1474636978-41435-2-git-send-email-glider@google.com \
    --to=glider@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=dvyukov@google.com \
    --cc=edumazet@google.com \
    --cc=kcc@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    /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

all inboxes | Powered by JetHome®