Skip to contentSkip to conversation

s390/monwriter: Reject buffer reuse with different data length

PATCH Superseded Gerald Schaefer 2 participants 2 messages Jun 23 s390

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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

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:

New issues:
- [Critical] The patch introduces an unconditional NULL pointer dereference when processing a MONWRITE_STOP_INTERVAL record.

Pre-existing issues:
- [High] MONWRITE_GEN_EVENT buffers are not properly accounted for in the global mon_buf_count, allowing an attacker to underflow the counter and bypass memory limits.
- [High] The global variable mon_buf_count is modified concurrently without global synchronization, risking corrupted counts.
--

commit d2476036fa25e1db9629a0b3a14cc2e8ed5a7d79
Author: Gerald Schaefer <[email protected]>

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.

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

Quoted 10 lines
drivers/s390/char/monwriter.c +2 -0
index eaeb4a6384d1f..84e6deed49130 100644
@@ -123,6 +123,9 @@ static int monwrite_new_hdr(struct mon_private *monpriv)
 			kfree(monbuf);
 			monbuf = NULL;
 		}
+		/* Data with buffer reuse must not change its length */
+		if (monbuf->hdr.datalen != monhdr->datalen)

[Severity: Critical]
Does this code cause a NULL pointer dereference?

If a user writes a valid MONWRITE_STOP_INTERVAL record, monbuf is freed and set to NULL immediately prior to this check. Evaluating monbuf->hdr.datalen here will unconditionally panic the kernel.

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

Quoted 3 lines
drivers/s390/char/monwriter.c +1 -0
+			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 does this code allow an underflow of mon_buf_count?

In monwrite_new_hdr(), if a user writes a MONWRITE_GEN_EVENT header:

monwrite_new_hdr()

    ...
    if (monhdr->mon_function != MONWRITE_GEN_EVENT)
        mon_buf_count++;

the buffer is added to monpriv->list but mon_buf_count is not incremented. If the user then skips writing the associated data and closes the file, monwrite_close() will unconditionally decrement mon_buf_count for that dangling buffer, underflowing the global limit counter to a negative value and allowing a bypass of the mon_max_bufs limit.

[Severity: High]
This is also a pre-existing issue, but is the global mon_buf_count variable safe from concurrent modifications?

It appears monwrite_close() decrements mon_buf_count while iterating over monpriv->list without holding any global synchronization locks:

monwrite_close()

    ...
    list_for_each_entry_safe(entry, next, &monpriv->list, list) {
        ...
        mon_buf_count--;
    }

Similarly, monwrite_new_hdr() modifies it while holding only a per-file instance thread_mutex. Can concurrent open, write, or close operations from multiple processes corrupt the global count?

Quoted 1 line
drivers/s390/char/monwriter.c +0 -0
 			return -ENOSPC;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1