All of lore.kernel.org
 help / color / mirror / Atom feed
From: laniel_francis@privacyrequired.com
To: linux-hardening@vger.kernel.org
Cc: dja@axtens.net, Francis Laniel <laniel_francis@privacyrequired.com>
Subject: [RFC][PATCH v3 3/5] Fortify string function strscpy.
Date: Wed, 21 Oct 2020 17:06:06 +0200	[thread overview]
Message-ID: <20201021150608.16469-4-laniel_francis@privacyrequired.com> (raw)
In-Reply-To: <20201021150608.16469-1-laniel_francis@privacyrequired.com>

From: Francis Laniel <laniel_francis@privacyrequired.com>

Fortified strscpy detects write overflows to dest.

Signed-off-by: Francis Laniel <laniel_francis@privacyrequired.com>
---
 include/linux/string.h | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 46e91d684c47..add7426ff718 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -6,6 +6,8 @@
 #include <linux/compiler.h>	/* for inline */
 #include <linux/types.h>	/* for size_t */
 #include <linux/stddef.h>	/* for NULL */
+#include <linux/bug.h>		/* for WARN_ON_ONCE */
+#include <linux/errno.h>	/* for E2BIG */
 #include <stdarg.h>
 #include <uapi/linux/string.h>
 
@@ -357,6 +359,42 @@ __FORTIFY_INLINE size_t strlcpy(char *p, const char *q, size_t size)
 	return ret;
 }
 
+/* defined after fortified strnlen to reuse it */
+extern ssize_t __real_strscpy(char *, const char *, size_t) __RENAME(strscpy);
+__FORTIFY_INLINE ssize_t strscpy(char *p, const char *q, size_t size)
+{
+	size_t len;
+	/*
+	 * Use 1 as second argument to guess only p size even and not the
+	 * surrounding struct size (in case it is embedded inside a struct).
+	 */
+	size_t p_size = __builtin_object_size(p, 1);
+
+	/*
+	 * If size can be known at compile time and is greater than
+	 * p_size, generate a compile time write overflow error.
+	 */
+	if (__builtin_constant_p(size) && size > p_size)
+		__write_overflow();
+
+	len = strnlen(q, size);
+	/*
+	 * strscpy handles read overflows by stop reading q when '\0' is
+	 * met.
+	 * We stick to this behavior here.
+	 */
+	len = (len >= size) ? size : len;
+
+	/* Otherwise generate a runtime write overflow error. */
+	if (len > p_size)
+		fortify_panic(__func__);
+	/*
+	 * Still use size as third argument to correctly compute max
+	 * inside strscpy.
+	 */
+	return __real_strscpy(p, q, size);
+}
+
 /* defined after fortified strlen and strnlen to reuse them */
 __FORTIFY_INLINE char *strncat(char *p, const char *q, __kernel_size_t count)
 {
-- 
2.20.1


  parent reply	other threads:[~2020-10-21 15:06 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-21 15:06 [RFC][PATCH v3 0/5] Fortify string function strscpy laniel_francis
2020-10-21 15:06 ` [PATCH v3 1/5] string.h: detect intra-object overflow in fortified string functions laniel_francis
2020-10-21 15:06 ` [PATCH v3 2/5] lkdtm: tests for FORTIFY_SOURCE laniel_francis
2020-10-21 15:06 ` laniel_francis [this message]
2020-10-24  5:04   ` [RFC][PATCH v3 3/5] Fortify string function strscpy Kees Cook
2020-10-24 10:36     ` Francis Laniel
2020-10-21 15:06 ` [RFC][PATCH v3 4/5] Add new file in LKDTM to test fortified strscpy laniel_francis
2020-10-24  5:23   ` Kees Cook
2020-10-24 10:19     ` Francis Laniel
2020-10-21 15:06 ` [RFC][PATCH v3 5/5] Correct wrong filenames in comment laniel_francis
2020-10-24  5:26   ` Kees Cook

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=20201021150608.16469-4-laniel_francis@privacyrequired.com \
    --to=laniel_francis@privacyrequired.com \
    --cc=dja@axtens.net \
    --cc=linux-hardening@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.