iommufd: Explicitize struct iommu_hwpt_pgfault padding
PATCH v2 1/1 iommufd: Explicitize struct iommu_hwpt_pgfault padding
Though the padding could be done by the compiler, add a 32-bit padding explicitly. Update iommufd_compose_fault_message to set it explicitly. Also, change the __u64 addr to __aligned_u64.
pahole result, before:
struct iommu_hwpt_pgfault {
__u32 flags; /* 0 4 */
__u32 dev_id; /* 4 4 */
__u32 pasid; /* 8 4 */
__u32 grpid; /* 12 4 */
__u32 perm; /* 16 4 */ /* XXX 4 bytes hole, try to pack */
__u64 addr; /* 24 8 */
__u32 length; /* 32 4 */
__u32 cookie; /* 36 4 */
/* size: 40, cachelines: 1, members: 8 */
/* sum members: 36, holes: 1, sum holes: 4 */
/* last cacheline: 40 bytes */
};pahole result, after:
struct iommu_hwpt_pgfault {
__u32 flags; /* 0 4 */
__u32 dev_id; /* 4 4 */
__u32 pasid; /* 8 4 */
__u32 grpid; /* 12 4 */
__u32 perm; /* 16 4 */
__u32 __reserved; /* 20 4 */
__u64 addr __attribute__((__aligned__(8))); /* 24 8 */
__u32 length; /* 32 4 */
__u32 cookie; /* 36 4 */ /* size: 40, cachelines: 1, members: 9 */
/* forced alignments: 1 */
/* last cacheline: 40 bytes */
} __attribute__((__aligned__(8)));--- drivers/iommu/iommufd/fault.c | 1 + include/uapi/linux/iommufd.h | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-)
drivers/iommu/iommufd/fault.c +1 -0
@@ -253,6 +253,7 @@ static void iommufd_compose_fault_message(struct iommu_fault *fault, hwpt_fault->pasid = fault->prm.pasid; hwpt_fault->grpid = fault->prm.grpid; hwpt_fault->perm = fault->prm.perm; + hwpt_fault->__reserved =0 ; hwpt_fault->addr = fault->prm.addr; hwpt_fault->length =0 ; hwpt_fault->cookie = cookie;
include/uapi/linux/iommufd.h +3 -1
@@ -868,6 +868,7 @@ enum iommu_hwpt_pgfault_perm {* @pasid: Process Address Space ID * @grpid: Page Request Group Index * @perm: Combination of enum iommu_hwpt_pgfault_perm +* @__reserved: Must be 0. * @addr: Fault address * @length: a hint of how much data the requestor is expecting to fetch. For * example, if the PRI initiator knows it is going to do a 10MB
@@ -883,7 +884,8 @@ struct iommu_hwpt_pgfault {__u32 pasid;__u32 grpid;__u32 perm; -__u64 addr; +__u32 __reserved; +__aligned_u64 addr;__u32 length;__u32 cookie; };
Signature · 1 line
2.34.1
drivers/iommu/iommufd/fault.c static void iommufd_compose_fault_message(struct iommu_fault *fault, · line 259
Quoted 10 lines · Nicolin Chen
drivers/iommu/iommufd/fault.c +1
-0@@ -253,6 +253,7 @@ static void iommufd_compose_fault_message(struct iommu_fault *fault, hwpt_fault->pasid = fault->prm.pasid; hwpt_fault->grpid = fault->prm.grpid; hwpt_fault->perm = fault->prm.perm; + hwpt_fault->__reserved =0 ; hwpt_fault->addr = fault->prm.addr; hwpt_fault->length =0 ; hwpt_fault->cookie = cookie;
Yikes, so it was leaking kernel stack memory through the padding too.
We should zero init the stack struct to be safe:
@@ -247,7 +247,7 @@ static ssize_t iommufd_fault_fops_read(struct file *filep, char __user *buf,
{
size_t fault_size = sizeof(struct iommu_hwpt_pgfault);
struct iommufd_fault *fault = filep->private_data;
- struct iommu_hwpt_pgfault data;
+ struct iommu_hwpt_pgfault data = {};
struct iommufd_device *idev;
struct iopf_group *group;
struct iopf_fault *iopf;I can fix it up if that is the only change
Jason
Quoted 26 lines · Jason Gunthorpe
drivers/iommu/iommufd/fault.c static void iommufd_compose_fault_message(struct iommu_fault *fault, · line 259
Quoted 10 lines · Nicolin Chen
drivers/iommu/iommufd/fault.c +1
-0@@ -253,6 +253,7 @@ static void iommufd_compose_fault_message(struct iommu_fault *fault, hwpt_fault->pasid = fault->prm.pasid; hwpt_fault->grpid = fault->prm.grpid; hwpt_fault->perm = fault->prm.perm; + hwpt_fault->__reserved =0 ; hwpt_fault->addr = fault->prm.addr; hwpt_fault->length =0 ; hwpt_fault->cookie = cookie;Yikes, so it was leaking kernel stack memory through the padding too.
We should zero init the stack struct to be safe:
(quoted diff) +1
-1@@ -247,7 +247,7 @@ static ssize_t iommufd_fault_fops_read(struct file *filep, char __user *buf, { size_t fault_size = sizeof(struct iommu_hwpt_pgfault); struct iommufd_fault *fault = filep->private_data; - struct iommu_hwpt_pgfault data; + struct iommu_hwpt_pgfault data = {}; struct iommufd_device *idev; struct iopf_group *group; struct iopf_fault *iopf;
Quoted 1 line
I can fix it up if that is the only change
Sending a v3.
I think having the zeroing in iommufd_fault_fops_read can drop the "__reserved = 0" iommufd_compose_fault_message.
Thanks
Nicolin
Quoted 3 lines · Nicolin Chen
Though the padding could be done by the compiler, add a 32-bit padding explicitly. Update
iommufd_compose_fault_messageto set it explicitly. Also, change the__u64addr to__aligned_u64.
Mention that this also leaks the content of kernel stack memory to userspace since it was never zeroing the padding.
Quoted 6 lines
Fixes c714f15860fc ("iommufd: Add fault and response message definitions")Signed-off-by Nicolin Chen <[email protected]>--- drivers/iommu/iommufd/fault.c | 1 + include/uapi/linux/iommufd.h | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-)
Jason