Skip to content

Commit feea65a

Browse files
committed
powerpc/powernv: Fix fortify source warnings in opal-prd.c
As reported by Mahesh & Aneesh, opal_prd_msg_notifier() triggers a FORTIFY_SOURCE warning: memcpy: detected field-spanning write (size 32) of single field "&item->msg" at arch/powerpc/platforms/powernv/opal-prd.c:355 (size 4) WARNING: CPU: 9 PID: 660 at arch/powerpc/platforms/powernv/opal-prd.c:355 opal_prd_msg_notifier+0x174/0x188 [opal_prd] NIP opal_prd_msg_notifier+0x174/0x188 [opal_prd] LR opal_prd_msg_notifier+0x170/0x188 [opal_prd] Call Trace: opal_prd_msg_notifier+0x170/0x188 [opal_prd] (unreliable) notifier_call_chain+0xc0/0x1b0 atomic_notifier_call_chain+0x2c/0x40 opal_message_notify+0xf4/0x2c0 This happens because the copy is targeting item->msg, which is only 4 bytes in size, even though the enclosing item was allocated with extra space following the msg. To fix the warning define struct opal_prd_msg with a union of the header and a flex array, and have the memcpy target the flex array. Reported-by: "Aneesh Kumar K.V" <[email protected]> Reported-by: Mahesh Salgaonkar <[email protected]> Tested-by: Mahesh Salgaonkar <[email protected]> Reviewed-by: Mahesh Salgaonkar <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Link: https://msgid.link/[email protected]
1 parent c265735 commit feea65a

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

arch/powerpc/platforms/powernv/opal-prd.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,20 @@
2424
#include <linux/uaccess.h>
2525

2626

27+
struct opal_prd_msg {
28+
union {
29+
struct opal_prd_msg_header header;
30+
DECLARE_FLEX_ARRAY(u8, data);
31+
};
32+
};
33+
2734
/*
2835
* The msg member must be at the end of the struct, as it's followed by the
2936
* message data.
3037
*/
3138
struct opal_prd_msg_queue_item {
32-
struct list_head list;
33-
struct opal_prd_msg_header msg;
39+
struct list_head list;
40+
struct opal_prd_msg msg;
3441
};
3542

3643
static struct device_node *prd_node;
@@ -156,7 +163,7 @@ static ssize_t opal_prd_read(struct file *file, char __user *buf,
156163
int rc;
157164

158165
/* we need at least a header's worth of data */
159-
if (count < sizeof(item->msg))
166+
if (count < sizeof(item->msg.header))
160167
return -EINVAL;
161168

162169
if (*ppos)
@@ -186,7 +193,7 @@ static ssize_t opal_prd_read(struct file *file, char __user *buf,
186193
return -EINTR;
187194
}
188195

189-
size = be16_to_cpu(item->msg.size);
196+
size = be16_to_cpu(item->msg.header.size);
190197
if (size > count) {
191198
err = -EINVAL;
192199
goto err_requeue;
@@ -352,7 +359,7 @@ static int opal_prd_msg_notifier(struct notifier_block *nb,
352359
if (!item)
353360
return -ENOMEM;
354361

355-
memcpy(&item->msg, msg->params, msg_size);
362+
memcpy(&item->msg.data, msg->params, msg_size);
356363

357364
spin_lock_irqsave(&opal_prd_msg_queue_lock, flags);
358365
list_add_tail(&item->list, &opal_prd_msg_queue);

0 commit comments

Comments
 (0)