if (ret != size)
return ret < 0 ? (int)ret : -1;
+ ff->offset += size;
return 0;
}
static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
{
- if (size > (ssize_t)ff->size - ff->offset)
- return -1;
-
memcpy(addr, ff->buf + ff->offset, size);
ff->offset += size;
return 0;
-
}
static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
{
+ /*
+ * Reject negative sizes, which on 32-bit can occur when a
+ * u32 >= 0x80000000 is passed as ssize_t. The cast to
+ * ssize_t is safe because perf_header__process_sections()
+ * validates that each section fits within the file size
+ * before any feature callback reaches here, and only
+ * feature sections (metadata like build IDs, topology, etc.)
+ * use this path — these cannot legitimately approach 2GB.
+ */
+ if (size < 0 || size > (ssize_t)ff->size - ff->offset)
+ return -1;
+
if (!ff->buf)
return __do_read_fd(ff, addr, size);
return __do_read_buf(ff, addr, size);
if (do_read_u32(ff, &len))
return NULL;
+ /* At least the null terminator. */
+ if (len < 1 || len > ff->size - ff->offset) {
+ pr_debug("do_read_string: invalid length %u (remaining %zu)\n",
+ len, (size_t)(ff->size - ff->offset));
+ return NULL;
+ }
+
buf = malloc(len);
if (!buf)
return NULL;
if (!__do_read(ff, buf, len)) {
/*
- * strings are padded by zeroes
- * thus the actual strlen of buf
- * may be less than len
+ * do_write_string() writes len including the null
+ * terminator, padded to NAME_ALIGN. Ensure the
+ * string is always null-terminated even if the file
+ * data has been tampered with.
*/
+ buf[len - 1] = '\0';
return buf;
}
static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
{
- if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
+ /* lseek fails in pipe mode — fall back to ff->offset */
+ off_t offset = lseek(ff->fd, 0, SEEK_CUR);
+
+ if (offset == (off_t)-1)
+ offset = ff->offset;
+
+ if (perf_header__read_build_ids(ff->ph, ff->fd, offset, ff->size))
pr_debug("Failed to read buildids, continuing...\n");
return 0;
}
ff = (struct feat_fd) {
.fd = fd,
.ph = ph,
+ .size = section->size,
};
if (!feat_ops[feat].full_only || hd->full)
int sec_size;
int feat;
int err;
+ struct stat st;
nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
if (!nr_sections)
if (err < 0)
goto out_free;
+ if (fstat(fd, &st) < 0) {
+ pr_err("Failed to stat the perf data file\n");
+ err = -1;
+ goto out_free;
+ }
+
for_each_set_bit(feat, header->adds_features, header->last_feat) {
+ /*
+ * FIXME: block devices have st_size == 0, so we skip
+ * bounds checking entirely. Historically perf never
+ * prevented using a block device as input, but it
+ * probably should — there's no valid use case for it
+ * and it bypasses all file-size validation.
+ */
+ if (S_ISREG(st.st_mode) &&
+ (sec->offset > (u64)st.st_size ||
+ sec->size > (u64)st.st_size - sec->offset)) {
+ pr_err("Feature %s (%d) section extends past EOF (offset=%" PRIu64 ", size=%" PRIu64 ", file=%" PRIu64 ")\n",
+ header_feat__name(feat), feat,
+ sec->offset, sec->size, (u64)st.st_size);
+ err = -1;
+ goto out_free;
+ }
err = process(sec++, header, feat, fd, data);
if (err < 0)
goto out_free;
.fd = fd,
.ph = ph,
.size = section->size,
- .offset = section->offset,
+ .offset = 0,
};
if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {