* [PATCH 0/5] powerpc/powernv: OPAL sysparam sysfs fixes
@ 2014-04-24 7:25 Joel Stanley
2014-04-24 7:25 ` [PATCH 1/5] powerpc/powernv: Fix sysparam sysfs error handling Joel Stanley
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: Joel Stanley @ 2014-04-24 7:25 UTC (permalink / raw)
To: benh; +Cc: neelegup, linuxppc-dev, paulus
These fix the issues I saw when testing the sysparam sysfs API. As some of the
parameters are not available on the machine I was using, the error paths in the
sysfs and OPAL code are tested, and were found to have some bugs.
Joel Stanley (5):
powerpc/powernv: Fix sysparam sysfs error handling
powerpc/powernv: Use ssize_t for sysparam return values
powerpc/powernv: Check sysfs size before copying
powerpc/powernv: Fix typos in sysparam code
powerpc/powernv: Check sysparam size before creation
arch/powerpc/platforms/powernv/opal-sysparam.c | 32 ++++++++++++++++++--------
1 file changed, 23 insertions(+), 9 deletions(-)
--
1.9.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 1/5] powerpc/powernv: Fix sysparam sysfs error handling
2014-04-24 7:25 [PATCH 0/5] powerpc/powernv: OPAL sysparam sysfs fixes Joel Stanley
@ 2014-04-24 7:25 ` Joel Stanley
2014-04-24 7:25 ` [PATCH 2/5] powerpc/powernv: Use ssize_t for sysparam return values Joel Stanley
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Joel Stanley @ 2014-04-24 7:25 UTC (permalink / raw)
To: benh; +Cc: neelegup, linuxppc-dev, paulus
When a sysparam query in OPAL returned a negative value (error code),
sysfs would spew out a decent chunk of memory; almost 64K more than
expected. This was traced to a sign/unsigned mix up in the OPAL sysparam
sysfs code at sys_param_show.
The return value of sys_param_show is a ssize_t, calculated using
return ret ? ret : attr->param_size;
Alan Modra explains:
"attr->param_size" is an unsigned int, "ret" an int, so the overall
expression has type unsigned int. Result is that ret is cast to
unsigned int before being cast to ssize_t.
Instead of using the ternary operator, set ret to the param_size if an
error is not detected. The same bug exists in the sysfs write callback;
this patch fixes it in the same way.
A note on debugging this next time: on my system gcc will warn about
this if compiled with -Wsign-compare, which is not enabled by -Wall,
only -Wextra.
Signed-off-by: Joel Stanley <joel@jms.id.au>
---
arch/powerpc/platforms/powernv/opal-sysparam.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/arch/powerpc/platforms/powernv/opal-sysparam.c b/arch/powerpc/platforms/powernv/opal-sysparam.c
index 0bd249a..cdaf145 100644
--- a/arch/powerpc/platforms/powernv/opal-sysparam.c
+++ b/arch/powerpc/platforms/powernv/opal-sysparam.c
@@ -121,9 +121,10 @@ static ssize_t sys_param_show(struct kobject *kobj,
memcpy(buf, param_data_buf, attr->param_size);
+ ret = attr->param_size;
out:
mutex_unlock(&opal_sysparam_mutex);
- return ret ? ret : attr->param_size;
+ return ret;
}
static ssize_t sys_param_store(struct kobject *kobj,
@@ -138,7 +139,9 @@ static ssize_t sys_param_store(struct kobject *kobj,
ret = opal_set_sys_param(attr->param_id, attr->param_size,
param_data_buf);
mutex_unlock(&opal_sysparam_mutex);
- return ret ? ret : count;
+ if (!ret)
+ ret = count;
+ return ret;
}
void __init opal_sys_param_init(void)
--
1.9.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 2/5] powerpc/powernv: Use ssize_t for sysparam return values
2014-04-24 7:25 [PATCH 0/5] powerpc/powernv: OPAL sysparam sysfs fixes Joel Stanley
2014-04-24 7:25 ` [PATCH 1/5] powerpc/powernv: Fix sysparam sysfs error handling Joel Stanley
@ 2014-04-24 7:25 ` Joel Stanley
2014-04-24 7:25 ` [PATCH 3/5] powerpc/powernv: Check sysfs size before copying Joel Stanley
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Joel Stanley @ 2014-04-24 7:25 UTC (permalink / raw)
To: benh; +Cc: neelegup, linuxppc-dev, paulus
The OPAL calls are returning int64_t values, which the sysparam code
stores in an int, and the sysfs callback returns ssize_t. Make code a
easier to read by consistently using ssize_t.
Signed-off-by: Joel Stanley <joel@jms.id.au>
---
arch/powerpc/platforms/powernv/opal-sysparam.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/arch/powerpc/platforms/powernv/opal-sysparam.c b/arch/powerpc/platforms/powernv/opal-sysparam.c
index cdaf145..a363c53 100644
--- a/arch/powerpc/platforms/powernv/opal-sysparam.c
+++ b/arch/powerpc/platforms/powernv/opal-sysparam.c
@@ -39,10 +39,11 @@ struct param_attr {
struct kobj_attribute kobj_attr;
};
-static int opal_get_sys_param(u32 param_id, u32 length, void *buffer)
+static ssize_t opal_get_sys_param(u32 param_id, u32 length, void *buffer)
{
struct opal_msg msg;
- int ret, token;
+ ssize_t ret;
+ int token;
token = opal_async_get_token_interruptible();
if (token < 0) {
@@ -59,7 +60,7 @@ static int opal_get_sys_param(u32 param_id, u32 length, void *buffer)
ret = opal_async_wait_response(token, &msg);
if (ret) {
- pr_err("%s: Failed to wait for the async response, %d\n",
+ pr_err("%s: Failed to wait for the async response, %zd\n",
__func__, ret);
goto out_token;
}
@@ -111,7 +112,7 @@ static ssize_t sys_param_show(struct kobject *kobj,
{
struct param_attr *attr = container_of(kobj_attr, struct param_attr,
kobj_attr);
- int ret;
+ ssize_t ret;
mutex_lock(&opal_sysparam_mutex);
ret = opal_get_sys_param(attr->param_id, attr->param_size,
@@ -132,7 +133,7 @@ static ssize_t sys_param_store(struct kobject *kobj,
{
struct param_attr *attr = container_of(kobj_attr, struct param_attr,
kobj_attr);
- int ret;
+ ssize_t ret;
mutex_lock(&opal_sysparam_mutex);
memcpy(param_data_buf, buf, count);
--
1.9.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 3/5] powerpc/powernv: Check sysfs size before copying
2014-04-24 7:25 [PATCH 0/5] powerpc/powernv: OPAL sysparam sysfs fixes Joel Stanley
2014-04-24 7:25 ` [PATCH 1/5] powerpc/powernv: Fix sysparam sysfs error handling Joel Stanley
2014-04-24 7:25 ` [PATCH 2/5] powerpc/powernv: Use ssize_t for sysparam return values Joel Stanley
@ 2014-04-24 7:25 ` Joel Stanley
2014-04-24 7:25 ` [PATCH 4/5] powerpc/powernv: Fix typos in sysparam code Joel Stanley
2014-04-24 7:25 ` [PATCH 5/5] powerpc/powernv: Check sysparam size before creation Joel Stanley
4 siblings, 0 replies; 6+ messages in thread
From: Joel Stanley @ 2014-04-24 7:25 UTC (permalink / raw)
To: benh; +Cc: neelegup, linuxppc-dev, paulus
The sysparam code currently uses the userspace supplied number of
bytes when memcpy()ing in to a local 64-byte buffer.
Limit the maximum number of bytes by the size of the buffer.
---
arch/powerpc/platforms/powernv/opal-sysparam.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/arch/powerpc/platforms/powernv/opal-sysparam.c b/arch/powerpc/platforms/powernv/opal-sysparam.c
index a363c53..21fdc0a 100644
--- a/arch/powerpc/platforms/powernv/opal-sysparam.c
+++ b/arch/powerpc/platforms/powernv/opal-sysparam.c
@@ -135,6 +135,10 @@ static ssize_t sys_param_store(struct kobject *kobj,
kobj_attr);
ssize_t ret;
+ /* MAX_PARAM_DATA_LEN is sizeof(param_data_buf) */
+ if (count > MAX_PARAM_DATA_LEN)
+ count = MAX_PARAM_DATA_LEN;
+
mutex_lock(&opal_sysparam_mutex);
memcpy(param_data_buf, buf, count);
ret = opal_set_sys_param(attr->param_id, attr->param_size,
--
1.9.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 4/5] powerpc/powernv: Fix typos in sysparam code
2014-04-24 7:25 [PATCH 0/5] powerpc/powernv: OPAL sysparam sysfs fixes Joel Stanley
` (2 preceding siblings ...)
2014-04-24 7:25 ` [PATCH 3/5] powerpc/powernv: Check sysfs size before copying Joel Stanley
@ 2014-04-24 7:25 ` Joel Stanley
2014-04-24 7:25 ` [PATCH 5/5] powerpc/powernv: Check sysparam size before creation Joel Stanley
4 siblings, 0 replies; 6+ messages in thread
From: Joel Stanley @ 2014-04-24 7:25 UTC (permalink / raw)
To: benh; +Cc: neelegup, linuxppc-dev, paulus
---
arch/powerpc/platforms/powernv/opal-sysparam.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/arch/powerpc/platforms/powernv/opal-sysparam.c b/arch/powerpc/platforms/powernv/opal-sysparam.c
index 21fdc0a..4b3367b 100644
--- a/arch/powerpc/platforms/powernv/opal-sysparam.c
+++ b/arch/powerpc/platforms/powernv/opal-sysparam.c
@@ -222,13 +222,13 @@ void __init opal_sys_param_init(void)
}
if (of_property_read_u32_array(sysparam, "param-len", size, count)) {
- pr_err("SYSPARAM: Missing propery param-len in the DT\n");
+ pr_err("SYSPARAM: Missing property param-len in the DT\n");
goto out_free_perm;
}
if (of_property_read_u8_array(sysparam, "param-perm", perm, count)) {
- pr_err("SYSPARAM: Missing propery param-perm in the DT\n");
+ pr_err("SYSPARAM: Missing property param-perm in the DT\n");
goto out_free_perm;
}
--
1.9.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 5/5] powerpc/powernv: Check sysparam size before creation
2014-04-24 7:25 [PATCH 0/5] powerpc/powernv: OPAL sysparam sysfs fixes Joel Stanley
` (3 preceding siblings ...)
2014-04-24 7:25 ` [PATCH 4/5] powerpc/powernv: Fix typos in sysparam code Joel Stanley
@ 2014-04-24 7:25 ` Joel Stanley
4 siblings, 0 replies; 6+ messages in thread
From: Joel Stanley @ 2014-04-24 7:25 UTC (permalink / raw)
To: benh; +Cc: neelegup, linuxppc-dev, paulus
The size of the sysparam sysfs files is determined from the device tree
at boot. However the buffer is hard coded to 64 bytes. If we encounter a
parameter that is larger than 64, or miss-parse the device tree, the
buffer will overflow when reading or writing to the parameter.
Check it at discovery time, and if the parameter is too large, do not
create a sysfs entry for it.
Signed-off-by: Joel Stanley <joel@jms.id.au>
---
arch/powerpc/platforms/powernv/opal-sysparam.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/arch/powerpc/platforms/powernv/opal-sysparam.c b/arch/powerpc/platforms/powernv/opal-sysparam.c
index 4b3367b..14231a5 100644
--- a/arch/powerpc/platforms/powernv/opal-sysparam.c
+++ b/arch/powerpc/platforms/powernv/opal-sysparam.c
@@ -241,6 +241,12 @@ void __init opal_sys_param_init(void)
/* For each of the parameters, populate the parameter attributes */
for (i = 0; i < count; i++) {
+ if (size[i] > MAX_PARAM_DATA_LEN) {
+ pr_warn("SYSPARAM: Not creating parameter %d as size "
+ "exceeds buffer length\n", i);
+ continue;
+ }
+
sysfs_attr_init(&attr[i].kobj_attr.attr);
attr[i].param_id = id[i];
attr[i].param_size = size[i];
--
1.9.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
end of thread, other threads:[~2014-04-24 7:26 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-04-24 7:25 [PATCH 0/5] powerpc/powernv: OPAL sysparam sysfs fixes Joel Stanley
2014-04-24 7:25 ` [PATCH 1/5] powerpc/powernv: Fix sysparam sysfs error handling Joel Stanley
2014-04-24 7:25 ` [PATCH 2/5] powerpc/powernv: Use ssize_t for sysparam return values Joel Stanley
2014-04-24 7:25 ` [PATCH 3/5] powerpc/powernv: Check sysfs size before copying Joel Stanley
2014-04-24 7:25 ` [PATCH 4/5] powerpc/powernv: Fix typos in sysparam code Joel Stanley
2014-04-24 7:25 ` [PATCH 5/5] powerpc/powernv: Check sysparam size before creation Joel Stanley
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).