Skip to content

Commit 8962842

Browse files
Ming Leiaxboe
authored andcommitted
blk-mq: avoid sysfs buffer overflow with too many CPU cores
It is reported that sysfs buffer overflow can be triggered if the system has too many CPU cores(>841 on 4K PAGE_SIZE) when showing CPUs of hctx via /sys/block/$DEV/mq/$N/cpu_list. Use snprintf to avoid the potential buffer overflow. This version doesn't change the attribute format, and simply stops showing CPU numbers if the buffer is going to overflow. Cc: stable@vger.kernel.org Fixes: 676141e("blk-mq: don't dump CPU -> hw queue map on driver load") Signed-off-by: Ming Lei <ming.lei@redhat.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 626fb73 commit 8962842

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

‎block/blk-mq-sysfs.c‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,25 @@ static ssize_t blk_mq_hw_sysfs_nr_reserved_tags_show(struct blk_mq_hw_ctx *hctx,
158158

159159
static ssize_t blk_mq_hw_sysfs_cpus_show(struct blk_mq_hw_ctx *hctx, char *page)
160160
{
161+
const size_t size = PAGE_SIZE - 1;
161162
unsigned int i, first = 1;
162-
ssize_t ret = 0;
163+
int ret = 0, pos = 0;
163164

164165
for_each_cpu(i, hctx->cpumask) {
165166
if (first)
166-
ret += sprintf(ret + page, "%u", i);
167+
ret = snprintf(pos + page, size - pos, "%u", i);
167168
else
168-
ret += sprintf(ret + page, ", %u", i);
169+
ret = snprintf(pos + page, size - pos, ", %u", i);
170+
171+
if (ret >= size - pos)
172+
break;
169173

170174
first = 0;
175+
pos += ret;
171176
}
172177

173-
ret += sprintf(ret + page, "\n");
174-
return ret;
178+
ret = snprintf(pos + page, size - pos, "\n");
179+
return pos + ret;
175180
}
176181

177182
static struct blk_mq_hw_ctx_sysfs_entry blk_mq_hw_sysfs_nr_tags = {

0 commit comments

Comments
 (0)