Skip to content

Commit fbbe545

Browse files
ryaoshodanshok
authored andcommitted
Introduce kmem_scnprintf()
`snprintf()` is meant to protect against buffer overflows, but operating on the buffer using its return value, possibly by calling it again, can cause a buffer overflow, because it will return how many characters it would have written if it had enough space even when it did not. In a number of places, we repeatedly call snprintf() by successively incrementing a buffer offset and decrementing a buffer length, by its return value. This is a potentially unsafe usage of `snprintf()` whenever the buffer length is reached. CodeQL complained about this. To fix this, we introduce `kmem_scnprintf()`, which will return 0 when the buffer is zero or the number of written characters, minus 1 to exclude the NULL character, when the buffer was too small. In all other cases, it behaves like snprintf(). The name is inspired by the Linux and XNU kernels' `scnprintf()`. The implementation was written before I thought to look at `scnprintf()` and had a good name for it, but it turned out to have identical semantics to the Linux kernel version. That lead to the name, `kmem_scnprintf()`. CodeQL only catches this issue in loops, so repeated use of snprintf() outside of a loop was not caught. As a result, a thorough audit of the codebase was done to examine all instances of `snprintf()` usage for potential problems and a few were caught. Fixes for them are included in this patch. Unfortunately, ZED is one of the places where `snprintf()` is potentially used incorrectly. Since using `kmem_scnprintf()` in it would require changing how it is linked, we modify its usage to make it safe, no matter what buffer length is used. In addition, there was a bug in the use of the return value where the NULL format character was not being written by pwrite(). That has been fixed. Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Richard Yao <[email protected]> Closes openzfs#14098
1 parent be82677 commit fbbe545

File tree

14 files changed

+116
-39
lines changed

14 files changed

+116
-39
lines changed

cmd/zed/zed_event.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,12 @@ _bump_event_queue_length(void)
139139
if (qlen == orig_qlen)
140140
goto done;
141141
wr = snprintf(qlen_buf, sizeof (qlen_buf), "%ld", qlen);
142+
if (wr >= sizeof (qlen_buf)) {
143+
wr = sizeof (qlen_buf) - 1;
144+
zed_log_msg(LOG_WARNING, "Truncation in %s()", __func__);
145+
}
142146

143-
if (pwrite(zzlm, qlen_buf, wr, 0) < 0)
147+
if (pwrite(zzlm, qlen_buf, wr + 1, 0) < 0)
144148
goto done;
145149

146150
zed_log_msg(LOG_WARNING, "Bumping queue length to %ld", qlen);

include/os/freebsd/spl/sys/kmem.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ extern char *kmem_asprintf(const char *, ...)
5757
extern char *kmem_vasprintf(const char *fmt, va_list ap)
5858
__attribute__((format(printf, 1, 0)));
5959

60+
extern int kmem_scnprintf(char *restrict str, size_t size,
61+
const char *restrict fmt, ...);
62+
6063
typedef struct kmem_cache {
6164
char kc_name[32];
6265
#if !defined(KMEM_DEBUG)

include/os/linux/spl/sys/kmem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ extern char *kmem_asprintf(const char *fmt, ...)
3838
extern char *kmem_strdup(const char *str);
3939
extern void kmem_strfree(char *str);
4040

41+
#define kmem_scnprintf scnprintf
42+
4143
/*
4244
* Memory allocation interfaces
4345
*/

include/sys/spa.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ typedef struct blkptr {
600600

601601
/*
602602
* This macro allows code sharing between zfs, libzpool, and mdb.
603-
* 'func' is either snprintf() or mdb_snprintf().
603+
* 'func' is either kmem_scnprintf() or mdb_snprintf().
604604
* 'ws' (whitespace) can be ' ' for single-line format, '\n' for multi-line.
605605
*/
606606

include/sys/zfs_context.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,9 @@ extern char *kmem_asprintf(const char *fmt, ...);
695695
#define kmem_strfree(str) kmem_free((str), strlen(str) + 1)
696696
#define kmem_strdup(s) strdup(s)
697697

698+
extern int kmem_scnprintf(char *restrict str, size_t size,
699+
const char *restrict fmt, ...);
700+
698701
/*
699702
* Hostname information
700703
*/

lib/libspl/os/linux/zone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ getzoneid(void)
4141

4242
int c = snprintf(path, sizeof (path), "/proc/self/ns/user");
4343
/* This API doesn't have any error checking... */
44-
if (c < 0)
44+
if (c < 0 || c >= sizeof (path))
4545
return (0);
4646

4747
ssize_t r = readlink(path, buf, sizeof (buf) - 1);

lib/libzfs/os/freebsd/libzfs_compat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ libzfs_error_init(int error)
202202
size_t msglen = sizeof (errbuf);
203203

204204
if (modfind("zfs") < 0) {
205-
size_t len = snprintf(msg, msglen, dgettext(TEXT_DOMAIN,
205+
size_t len = kmem_scnprintf(msg, msglen, dgettext(TEXT_DOMAIN,
206206
"Failed to load %s module: "), ZFS_KMOD);
207207
msg += len;
208208
msglen -= len;

lib/libzpool/kernel.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,35 @@ kmem_asprintf(const char *fmt, ...)
956956
return (buf);
957957
}
958958

959+
/*
960+
* kmem_scnprintf() will return the number of characters that it would have
961+
* printed whenever it is limited by value of the size variable, rather than
962+
* the number of characters that it did print. This can cause misbehavior on
963+
* subsequent uses of the return value, so we define a safe version that will
964+
* return the number of characters actually printed, minus the NULL format
965+
* character. Subsequent use of this by the safe string functions is safe
966+
* whether it is snprintf(), strlcat() or strlcpy().
967+
*/
968+
int
969+
kmem_scnprintf(char *restrict str, size_t size, const char *restrict fmt, ...)
970+
{
971+
int n;
972+
va_list ap;
973+
974+
/* Make the 0 case a no-op so that we do not return -1 */
975+
if (size == 0)
976+
return (0);
977+
978+
va_start(ap, fmt);
979+
n = vsnprintf(str, size, fmt, ap);
980+
va_end(ap);
981+
982+
if (n >= size)
983+
n = size - 1;
984+
985+
return (n);
986+
}
987+
959988
zfs_file_t *
960989
zfs_onexit_fd_hold(int fd, minor_t *minorp)
961990
{

module/os/freebsd/spl/spl_string.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,33 @@ kmem_strfree(char *str)
105105
ASSERT3P(str, !=, NULL);
106106
kmem_free(str, strlen(str) + 1);
107107
}
108+
109+
/*
110+
* kmem_scnprintf() will return the number of characters that it would have
111+
* printed whenever it is limited by value of the size variable, rather than
112+
* the number of characters that it did print. This can cause misbehavior on
113+
* subsequent uses of the return value, so we define a safe version that will
114+
* return the number of characters actually printed, minus the NULL format
115+
* character. Subsequent use of this by the safe string functions is safe
116+
* whether it is snprintf(), strlcat() or strlcpy().
117+
*/
118+
119+
int
120+
kmem_scnprintf(char *restrict str, size_t size, const char *restrict fmt, ...)
121+
{
122+
int n;
123+
va_list ap;
124+
125+
/* Make the 0 case a no-op so that we do not return -1 */
126+
if (size == 0)
127+
return (0);
128+
129+
va_start(ap, fmt);
130+
n = vsnprintf(str, size, fmt, ap);
131+
va_end(ap);
132+
133+
if (n >= size)
134+
n = size - 1;
135+
136+
return (n);
137+
}

module/os/linux/zfs/zfs_sysfs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,11 @@ zprop_sysfs_show(const char *attr_name, const zprop_desc_t *property,
279279

280280
for (int i = 0; i < ARRAY_SIZE(type_map); i++) {
281281
if (type_map[i].ztm_type & property->pd_types) {
282-
len += snprintf(buf + len, buflen - len, "%s ",
283-
type_map[i].ztm_name);
282+
len += kmem_scnprintf(buf + len, buflen - len,
283+
"%s ", type_map[i].ztm_name);
284284
}
285285
}
286-
len += snprintf(buf + len, buflen - len, "\n");
286+
len += kmem_scnprintf(buf + len, buflen - len, "\n");
287287
return (len);
288288
}
289289

module/zfs/dataset_kstats.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,13 @@ dataset_kstats_create(dataset_kstats_t *dk, objset_t *objset)
128128
" snprintf() for kstat name returned %d",
129129
(unsigned long long)dmu_objset_id(objset), n);
130130
return (SET_ERROR(EINVAL));
131+
} else if (n >= KSTAT_STRLEN) {
132+
zfs_dbgmsg("failed to create dataset kstat for objset %lld: "
133+
"kstat name length (%d) exceeds limit (%d)",
134+
(unsigned long long)dmu_objset_id(objset),
135+
n, KSTAT_STRLEN);
136+
return (SET_ERROR(ENAMETOOLONG));
131137
}
132-
ASSERT3U(n, <, KSTAT_STRLEN);
133138

134139
kstat_t *kstat = kstat_create(kstat_module_name, 0, kstat_name,
135140
"dataset", KSTAT_TYPE_NAMED,

module/zfs/spa_misc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ snprintf_blkptr(char *buf, size_t buflen, const blkptr_t *bp)
15361536
compress = zio_compress_table[BP_GET_COMPRESS(bp)].ci_name;
15371537
}
15381538

1539-
SNPRINTF_BLKPTR(snprintf, ' ', buf, buflen, bp, type, checksum,
1539+
SNPRINTF_BLKPTR(kmem_scnprintf, ' ', buf, buflen, bp, type, checksum,
15401540
compress);
15411541
}
15421542

module/zfs/vdev_raidz_math.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -285,17 +285,17 @@ raidz_math_kstat_headers(char *buf, size_t size)
285285
{
286286
ASSERT3U(size, >=, RAIDZ_KSTAT_LINE_LEN);
287287

288-
ssize_t off = snprintf(buf, size, "%-17s", "implementation");
288+
ssize_t off = kmem_scnprintf(buf, size, "%-17s", "implementation");
289289

290290
for (int i = 0; i < ARRAY_SIZE(raidz_gen_name); i++)
291-
off += snprintf(buf + off, size - off, "%-16s",
291+
off += kmem_scnprintf(buf + off, size - off, "%-16s",
292292
raidz_gen_name[i]);
293293

294294
for (int i = 0; i < ARRAY_SIZE(raidz_rec_name); i++)
295-
off += snprintf(buf + off, size - off, "%-16s",
295+
off += kmem_scnprintf(buf + off, size - off, "%-16s",
296296
raidz_rec_name[i]);
297297

298-
(void) snprintf(buf + off, size - off, "\n");
298+
(void) kmem_scnprintf(buf + off, size - off, "\n");
299299

300300
return (0);
301301
}
@@ -311,34 +311,35 @@ raidz_math_kstat_data(char *buf, size_t size, void *data)
311311
ASSERT3U(size, >=, RAIDZ_KSTAT_LINE_LEN);
312312

313313
if (cstat == fstat) {
314-
off += snprintf(buf + off, size - off, "%-17s", "fastest");
314+
off += kmem_scnprintf(buf + off, size - off, "%-17s",
315+
"fastest");
315316

316317
for (i = 0; i < ARRAY_SIZE(raidz_gen_name); i++) {
317318
int id = fstat->gen[i];
318-
off += snprintf(buf + off, size - off, "%-16s",
319+
off += kmem_scnprintf(buf + off, size - off, "%-16s",
319320
raidz_supp_impl[id]->name);
320321
}
321322
for (i = 0; i < ARRAY_SIZE(raidz_rec_name); i++) {
322323
int id = fstat->rec[i];
323-
off += snprintf(buf + off, size - off, "%-16s",
324+
off += kmem_scnprintf(buf + off, size - off, "%-16s",
324325
raidz_supp_impl[id]->name);
325326
}
326327
} else {
327328
ptrdiff_t id = cstat - raidz_impl_kstats;
328329

329-
off += snprintf(buf + off, size - off, "%-17s",
330+
off += kmem_scnprintf(buf + off, size - off, "%-17s",
330331
raidz_supp_impl[id]->name);
331332

332333
for (i = 0; i < ARRAY_SIZE(raidz_gen_name); i++)
333-
off += snprintf(buf + off, size - off, "%-16llu",
334+
off += kmem_scnprintf(buf + off, size - off, "%-16llu",
334335
(u_longlong_t)cstat->gen[i]);
335336

336337
for (i = 0; i < ARRAY_SIZE(raidz_rec_name); i++)
337-
off += snprintf(buf + off, size - off, "%-16llu",
338+
off += kmem_scnprintf(buf + off, size - off, "%-16llu",
338339
(u_longlong_t)cstat->rec[i]);
339340
}
340341

341-
(void) snprintf(buf + off, size - off, "\n");
342+
(void) kmem_scnprintf(buf + off, size - off, "\n");
342343

343344
return (0);
344345
}

module/zfs/zfs_chksum.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ chksum_kstat_headers(char *buf, size_t size)
8181
{
8282
ssize_t off = 0;
8383

84-
off += snprintf(buf + off, size, "%-23s", "implementation");
85-
off += snprintf(buf + off, size - off, "%8s", "1k");
86-
off += snprintf(buf + off, size - off, "%8s", "4k");
87-
off += snprintf(buf + off, size - off, "%8s", "16k");
88-
off += snprintf(buf + off, size - off, "%8s", "64k");
89-
off += snprintf(buf + off, size - off, "%8s", "256k");
90-
off += snprintf(buf + off, size - off, "%8s", "1m");
91-
off += snprintf(buf + off, size - off, "%8s", "4m");
92-
(void) snprintf(buf + off, size - off, "%8s\n", "16m");
84+
off += kmem_scnprintf(buf + off, size, "%-23s", "implementation");
85+
off += kmem_scnprintf(buf + off, size - off, "%8s", "1k");
86+
off += kmem_scnprintf(buf + off, size - off, "%8s", "4k");
87+
off += kmem_scnprintf(buf + off, size - off, "%8s", "16k");
88+
off += kmem_scnprintf(buf + off, size - off, "%8s", "64k");
89+
off += kmem_scnprintf(buf + off, size - off, "%8s", "256k");
90+
off += kmem_scnprintf(buf + off, size - off, "%8s", "1m");
91+
off += kmem_scnprintf(buf + off, size - off, "%8s", "4m");
92+
(void) kmem_scnprintf(buf + off, size - off, "%8s\n", "16m");
9393

9494
return (0);
9595
}
@@ -102,23 +102,23 @@ chksum_kstat_data(char *buf, size_t size, void *data)
102102
char b[24];
103103

104104
cs = (chksum_stat_t *)data;
105-
snprintf(b, 23, "%s-%s", cs->name, cs->impl);
106-
off += snprintf(buf + off, size - off, "%-23s", b);
107-
off += snprintf(buf + off, size - off, "%8llu",
105+
kmem_scnprintf(b, 23, "%s-%s", cs->name, cs->impl);
106+
off += kmem_scnprintf(buf + off, size - off, "%-23s", b);
107+
off += kmem_scnprintf(buf + off, size - off, "%8llu",
108108
(u_longlong_t)cs->bs1k);
109-
off += snprintf(buf + off, size - off, "%8llu",
109+
off += kmem_scnprintf(buf + off, size - off, "%8llu",
110110
(u_longlong_t)cs->bs4k);
111-
off += snprintf(buf + off, size - off, "%8llu",
111+
off += kmem_scnprintf(buf + off, size - off, "%8llu",
112112
(u_longlong_t)cs->bs16k);
113-
off += snprintf(buf + off, size - off, "%8llu",
113+
off += kmem_scnprintf(buf + off, size - off, "%8llu",
114114
(u_longlong_t)cs->bs64k);
115-
off += snprintf(buf + off, size - off, "%8llu",
115+
off += kmem_scnprintf(buf + off, size - off, "%8llu",
116116
(u_longlong_t)cs->bs256k);
117-
off += snprintf(buf + off, size - off, "%8llu",
117+
off += kmem_scnprintf(buf + off, size - off, "%8llu",
118118
(u_longlong_t)cs->bs1m);
119-
off += snprintf(buf + off, size - off, "%8llu",
119+
off += kmem_scnprintf(buf + off, size - off, "%8llu",
120120
(u_longlong_t)cs->bs4m);
121-
(void) snprintf(buf + off, size - off, "%8llu\n",
121+
(void) kmem_scnprintf(buf + off, size - off, "%8llu\n",
122122
(u_longlong_t)cs->bs16m);
123123

124124
return (0);

0 commit comments

Comments
 (0)