Skip to contentSkip to conversation

s390/monwriter: Reject buffer reuse with different data length

PATCH In review Gerald Schaefer 3 participants 3 messages Jun 23 – Jun 24 s390

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 s390/monwriter: Reject buffer reuse with different data length

When data buffers are reused, e.g. for interval sample records, the first record determines the data length, and the size of the buffer for user copy. Current monwriter code does not check if the data length was changed for subsequent records, which also would never happen for valid user programs.

However, a malicious user could change the data length, resulting in out of bounds user copy to the kernel buffer, and memory corruption. By default, the monwriter misc device is created with root-only permissions, so practical impact is typically low.

Fix this by checking for changed data length and rejecting such records.

---
 drivers/s390/char/monwriter.c | 3 +++
 1 file changed, 3 insertions(+)

---
Changes in v2:
- Move check into "else if", to avoid NULL pointer dereference after MONWRITE_STOP_INTERVAL.

drivers/s390/char/monwriter.c +3 -0
index eaeb4a6384d1..ecf121a87f88 100644
@@ -122,6 +122,9 @@ static int monwrite_new_hdr(struct mon_private *monpriv)
 			kfree(monbuf->data);
 			kfree(monbuf);
 			monbuf = NULL;
+		} else if (monbuf->hdr.datalen != monhdr->datalen) {
+			/* Data with buffer reuse must not change its length */
+			return -EINVAL;
 		}
 	} else if (monhdr->mon_function != MONWRITE_STOP_INTERVAL) {
 		if (mon_buf_count >= mon_max_bufs)

base-commit: f31c00c377ccf07c85442712f7c940a855cb3371

Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [High] Stranded MONWRITE_GEN_EVENT buffers cause mon_buf_count to underflow on close, allowing unbounded kernel memory allocation.
- [High] The global variable mon_buf_count is updated concurrently without any synchronization, leading to a data race.
- [High] Asynchronous teardown failure in monwrite_diag leads to hypervisor Use-After-Free/Information Leak. --

commit eaf3c589ff6055b82f62fc5d169905bbb8fd5129
Author: Gerald Schaefer <[email protected]>
Subject: s390/monwriter: Reject buffer reuse with different data length

This commit fixes an issue where reusing data buffers with different data lengths could lead to an out-of-bounds user copy. It introduces a check in monwrite_new_hdr() to reject interval sample records if the new data length does not match the original buffer length.

drivers/s390/char/monwriter.c static int monwrite_new_hdr(struct mon_private *monpriv) · line 122

Quoted 6 lines
drivers/s390/char/monwriter.c +0 -0
index eaeb4a6384d1f..ecf121a87f88f 100644
@@ -122,6 +122,9 @@ static int monwrite_new_hdr(struct mon_private *monpriv)
 			kfree(monbuf->data);

[Severity: High]
This is a pre-existing issue, but does this code risk a hypervisor use-after-free or information leak if monwrite_diag() fails?

If kmalloc() fails inside monwrite_diag() during the teardown phase, it returns -ENOMEM without issuing the APPLDATA_STOP_REC instruction to the hypervisor. Because the error is ignored here and the data buffer is freed unconditionally, the hypervisor will continue reading the freed kernel memory.

drivers/s390/char/monwriter.c static int monwrite_new_hdr(struct mon_private *monpriv) · line 130

Quoted 8 lines
drivers/s390/char/monwriter.c +3 -0
 			kfree(monbuf);
 			monbuf = NULL;
+		} else if (monbuf->hdr.datalen != monhdr->datalen) {
+			/* Data with buffer reuse must not change its length */
+			return -EINVAL;
 		}
 	} else if (monhdr->mon_function != MONWRITE_STOP_INTERVAL) {
 		if (mon_buf_count >= mon_max_bufs)

[Severity: High]
This is a pre-existing issue, but is there a data race on the global mon_buf_count variable?

Multiple threads opening /dev/monwriter can concurrently execute monwrite_write() or monwrite_close() using their own per-file-descriptor thread_mutex. Because mon_buf_count is updated and checked without a global lock, concurrent modifications can corrupt the counter and allow the buffer limit to be bypassed.

[Severity: High]
This is a pre-existing issue, but can this logic lead to a mon_buf_count underflow?

If a user writes a MONWRITE_GEN_EVENT header with a data length of 0, monwrite_write() will skip calling monwrite_new_data(), leaving the buffer permanently on the monpriv->list. Since mon_buf_count is not incremented for MONWRITE_GEN_EVENT buffers:

drivers/s390/char/monwriter.c:monwrite_new_hdr() {
        ...
        if (monhdr->mon_function != MONWRITE_GEN_EVENT)
                mon_buf_count++;
        ...
}

When the file descriptor is closed, monwrite_close() iterates the list and unconditionally decrements mon_buf_count for every entry:

drivers/s390/char/monwriter.c:monwrite_close() {
        ...
        mon_buf_count--;
        list_del(&entry->list);
        ...
}

This underflows the global counter into negative values, effectively disabling the mon_max_bufs limit check for future writes.

Quoted 1 line

return -ENOSPC;

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer Reviewed-by on v2 1/1
#2 lore raw
In reply to Gerald Schaefer

Am 23.06.26 um 19:44 schrieb Gerald Schaefer:

Quoted 11 lines

When data buffers are reused, e.g. for interval sample records, the first record determines the data length, and the size of the buffer for user copy. Current monwriter code does not check if the data length was changed for subsequent records, which also would never happen for valid user programs.

However, a malicious user could change the data length, resulting in out of bounds user copy to the kernel buffer, and memory corruption. By default, the monwriter misc device is created with root-only permissions, so practical impact is typically low.

Quoted 4 lines

Fix this by checking for changed data length and rejecting such records.