Skip to content

Commit 35aae18

Browse files
nathanlynchmpe
authored andcommitted
powerpc/pseries/papr-sysparm: Validate buffer object lengths
The ability to get and set system parameters will be exposed to user space, so let's get a little more strict about malformed papr_sysparm_buf objects. * Create accessors for the length field of struct papr_sysparm_buf. The length is always stored in MSB order and this is better than spreading the necessary conversions all over. * Reject attempts to submit invalid buffers to RTAS. * Warn if RTAS returns a buffer with an invalid length, clamping the returned length to a safe value that won't overrun the buffer. These are meant as precautionary measures to mitigate both firmware and kernel bugs in this area, should they arise, but I am not aware of any. Signed-off-by: Nathan Lynch <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Link: https://msgid.link/20231212-papr-sys_rtas-vs-lockdown-v6-10-e9eafd0c8c6c@linux.ibm.com
1 parent 514f6ff commit 35aae18

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

arch/powerpc/platforms/pseries/papr-sysparm.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,46 @@ void papr_sysparm_buf_free(struct papr_sysparm_buf *buf)
2323
kfree(buf);
2424
}
2525

26+
static size_t papr_sysparm_buf_get_length(const struct papr_sysparm_buf *buf)
27+
{
28+
return be16_to_cpu(buf->len);
29+
}
30+
31+
static void papr_sysparm_buf_set_length(struct papr_sysparm_buf *buf, size_t length)
32+
{
33+
WARN_ONCE(length > sizeof(buf->val),
34+
"bogus length %zu, clamping to safe value", length);
35+
length = min(sizeof(buf->val), length);
36+
buf->len = cpu_to_be16(length);
37+
}
38+
39+
/*
40+
* For use on buffers returned from ibm,get-system-parameter before
41+
* returning them to callers. Ensures the encoded length of valid data
42+
* cannot overrun buf->val[].
43+
*/
44+
static void papr_sysparm_buf_clamp_length(struct papr_sysparm_buf *buf)
45+
{
46+
papr_sysparm_buf_set_length(buf, papr_sysparm_buf_get_length(buf));
47+
}
48+
49+
/*
50+
* Perform some basic diligence on the system parameter buffer before
51+
* submitting it to RTAS.
52+
*/
53+
static bool papr_sysparm_buf_can_submit(const struct papr_sysparm_buf *buf)
54+
{
55+
/*
56+
* Firmware ought to reject buffer lengths that exceed the
57+
* maximum specified in PAPR, but there's no reason for the
58+
* kernel to allow them either.
59+
*/
60+
if (papr_sysparm_buf_get_length(buf) > sizeof(buf->val))
61+
return false;
62+
63+
return true;
64+
}
65+
2666
/**
2767
* papr_sysparm_get() - Retrieve the value of a PAPR system parameter.
2868
* @param: PAPR system parameter token as described in
@@ -63,6 +103,9 @@ int papr_sysparm_get(papr_sysparm_t param, struct papr_sysparm_buf *buf)
63103
if (token == RTAS_UNKNOWN_SERVICE)
64104
return -ENOENT;
65105

106+
if (!papr_sysparm_buf_can_submit(buf))
107+
return -EINVAL;
108+
66109
work_area = rtas_work_area_alloc(sizeof(*buf));
67110

68111
memcpy(rtas_work_area_raw_buf(work_area), buf, sizeof(*buf));
@@ -77,6 +120,7 @@ int papr_sysparm_get(papr_sysparm_t param, struct papr_sysparm_buf *buf)
77120
case 0:
78121
ret = 0;
79122
memcpy(buf, rtas_work_area_raw_buf(work_area), sizeof(*buf));
123+
papr_sysparm_buf_clamp_length(buf);
80124
break;
81125
case -3: /* parameter not implemented */
82126
ret = -EOPNOTSUPP;
@@ -115,6 +159,9 @@ int papr_sysparm_set(papr_sysparm_t param, const struct papr_sysparm_buf *buf)
115159
if (token == RTAS_UNKNOWN_SERVICE)
116160
return -ENOENT;
117161

162+
if (!papr_sysparm_buf_can_submit(buf))
163+
return -EINVAL;
164+
118165
work_area = rtas_work_area_alloc(sizeof(*buf));
119166

120167
memcpy(rtas_work_area_raw_buf(work_area), buf, sizeof(*buf));

0 commit comments

Comments
 (0)