mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Tejun Heo <tj@kernel.org>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	linux-kernel@vger.kernel.org
Subject: [PATCH] kernfs: Fix kernfs_name_compare
Date: Thu,  4 Dec 2014 17:19:34 +0100	[thread overview]
Message-ID: <1417709976-31551-1-git-send-email-linux@rasmusvillemoes.dk> (raw)

If the void pointers ns and kn->ns happen to differ by a multiple of
2^32, kernfs_name_compare returns 0, falsely reporting a match to the
caller.

If the return type was changed to long, returning ns - kn->ns would
still be wrong (see acbbe6fbb240 "kcmp: fix standard comparison bug"),
and it would also break the primary comparison of the hashes: The
expression hash - kn->hash has type unsigned int; on 64-bit, long is
perfectly capable of representing all unsigned int values, hence the
return value would always be positive if the hashes are
different. Since the latter is slightly subtle and since returning
hash - kn->hash in the first place is only ok because the hashes are
restricted to 31 bits, add a comment explaining that.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 fs/kernfs/dir.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
index 1c771931bb60..64959716936d 100644
--- a/fs/kernfs/dir.c
+++ b/fs/kernfs/dir.c
@@ -201,10 +201,17 @@ static unsigned int kernfs_name_hash(const char *name, const void *ns)
 static int kernfs_name_compare(unsigned int hash, const char *name,
 			       const void *ns, const struct kernfs_node *kn)
 {
+	/*
+	 * This is ok because the hash values are restricted to [0,
+	 * 2^31-1] and because we are returning int (it would be wrong
+	 * had the return type been wider!).
+	 */
 	if (hash != kn->hash)
 		return hash - kn->hash;
-	if (ns != kn->ns)
-		return ns - kn->ns;
+	if (ns < kn->ns)
+		return -1;
+	if (ns > kn->ns)
+		return 1;
 	return strcmp(name, kn->name);
 }
 
-- 
2.0.4


             reply	other threads:[~2014-12-04 16:19 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-04 16:19 Rasmus Villemoes [this message]
2014-12-04 16:53 ` Tejun Heo
2014-12-05 22:41 ` [PATCH v2] " Rasmus Villemoes
2015-01-05 14:13   ` Tejun Heo
2015-01-05 16:14     ` Greg Kroah-Hartman

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=1417709976-31551-1-git-send-email-linux@rasmusvillemoes.dk \
    --to=linux@rasmusvillemoes.dk \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tj@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

all inboxes | Powered by JetHome®