Skip to contentSkip to conversation

bcache: A block layer cache

DISCUSSION Dan Carpenter 2 participants 3 messages Apr 1, 2013 – Apr 5, 2013 1 message not in the archive bcache (block layer cache)

lore
Share & tools
lore
#0 lore raw

Hello Kent Overstreet,

The patch cafe56359144: "bcache: A block layer cache" from Mar 23, 2013, leads to the following warning:
"drivers/md/bcache/util.c:92 hprint()
error: format string overflow. buf_size: 3 length: 4"

drivers/md/bcache/util.c

    77  ssize_t hprint(char *buf, int64_t v)
    78  {
    79          static const char units[] = "?kMGTPEZY";
    80          char dec[3] = "";
    81          int u, t = 0;
    82  
    83          for (u = 0; v >= 1024 || v <= -1024; u++) {
    84                  t = v & ~(~0 << 10);
    85                  v >>= 10;
    86          }
    87  
    88          if (!u)
    89                  return sprintf(buf, "%llu", v);
    90  
    91          if (v < 100 && v > -100)
    92                  sprintf(dec, ".%i", t / 100);

For example, hprint(buf, 2024) will cause a one char buffer overflow.

    93  
    94          return sprintf(buf, "%lli%s%c", v, dec, units[u]);

regards,
dan carpenter

A message that is not in the archive (sent off-list or not yet received): 20130401193740.GA32549-dZEljifmRObu9KfB+GxooP8+0UxHXcjY@public.gmane.org

#2 lore raw

Slightly unrelated, it's weird that bcache has KEY_START() and START_KEY(). Maybe FIRST_KEY() would be a better name for START_KEY()?

regards,
dan carpenter

#3 lore raw
In reply to a message not in the archive
Quoted 6 lines · Dan Carpenter

Hello Kent Overstreet,

The patch cafe56359144: "bcache: A block layer cache" from Mar 23, 2013, leads to the following warning:
"drivers/md/bcache/util.c:92 hprint()
error: format string overflow. buf_size: 3 length: 4"

Thanks - fixed

commit c20bbfc0e049ca785bb19419121091e4fd419886

Author: Kent Overstreet <[email protected]>
Date:   Fri Apr 5 14:12:28 2013 -0700

bcache: Fix a format string overflow

drivers/md/bcache/util.c +2 -2
index a9619d8..da3a99e 100644
@@ -76,7 +76,7 @@ STRTO_H(strtoull, unsigned long long)
 ssize_t bch_hprint(char *buf, int64_t v)
 {
 	static const char units[] = "?kMGTPEZY";
-	char dec[3] = "";
+	char dec[4] = "";
 	int u, t = 0;
 
 	for (u = 0; v >= 1024 || v <= -1024; u++) {
@@ -88,7 +88,7 @@ ssize_t bch_hprint(char *buf, int64_t v)
 		return sprintf(buf, "%llu", v);
 
 	if (v < 100 && v > -100)
-		sprintf(dec, ".%i", t / 100);
+		snprintf(dec, sizeof(dec), ".%i", t / 100);
 
 	return sprintf(buf, "%lli%s%c", v, dec, units[u]);
 }