firmware: arm_ffa: honor descriptor size in PARTITION_INFO_GET_REGS
PATCH 1/1 firmware: arm_ffa: honor descriptor size in PARTITION_INFO_GET_REGS
__ffa_partition_info_get_regs() walks the response with a hardcoded 24-byte stride (regs += 3) even though the SPMC tells us the actual per-descriptor size via PARTITION_INFO_SZ in x2[63:48]. The size is read into buf_sz and then thrown away.
That works while every SPMC returns the FF-A v1.1 layout, but it falls apart against a v1.3 SPMC returning the 48-byte descriptor. The loop strides over half a descriptor at a time and ends up parsing every other entry from a slice of two adjacent ones.
The FF-A spec (v1.2, section 18.5) says that the producer should report the descriptor size, and the consumer is supposed to stride by that size and ignore any trailing fields it doesn't understand. The non-REGS path (__ffa_partition_info_get) does this already, and the REGS path should match.
Use buf_sz for the stride, and bail out with -EPROTO if the SPMC reports something we can't safely walk.
--- drivers/firmware/arm_ffa/driver.c | 35 ++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-)
drivers/firmware/arm_ffa/driver.c +32 -3
@@ -321,6 +321,22 @@ __ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,#define PART_INFO_ID (x) ((u16)(FIELD_GET(PART_INFO_ID_MASK, (x))))#define PART_INFO_EXEC_CXT (x) ((u16)(FIELD_GET(PART_INFO_EXEC_CXT_MASK, (x))))#define PART_INFO_PROPERTIES (x) ((u32)(FIELD_GET(PART_INFO_PROPS_MASK, (x)))) + +/* +* FF-A v1.2 section 13.9 Table 13.40: registers x3..x17 carry the partition +* descriptors, i.e. 15 u64 of payload per FFA_PARTITION_INFO_GET_REGS call. +*/ +#define FFA_PART_INFO_REGS_PAYLOAD_U64 15 + +/* +* FF-A v1.1 partition information descriptor (FF-A v1.2 section 6.2.1 +* Table 6.1): id (2) + exec_ctxt (2) + properties (4) + UUID (16) = 24 +* bytes. This is the minimum size the SPMC must report; the kernel reads +* exactly these fields and ignores any trailing ones per the forward- +* compatibility rules in FF-A v1.2 section 18.5. +*/ +#define FFA_PART_INFO_DESC_V1_1_SZ 24 +static int __ffa_partition_info_get_regs (u32 uuid0,u32 uuid1,u32 uuid2,u32 uuid3,struct ffa_partition_info *buffer,int num_parts)
@@ -353,8 +369,21 @@ __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3, cur_idx =CURRENT_INDEX (partition_info.a2); tag =UUID_INFO_TAG (partition_info.a2); buf_sz =PARTITION_INFO_SZ (partition_info.a2); -if (buf_sz >sizeof (*buffer)) - buf_sz =sizeof (*buffer); + +/* +* Per FF-A v1.2 section 18.5 the SPMC reports its per- +* descriptor size and consumers must stride by that size, +* consuming only the fields they understand and ignoring any +* trailing ones. Reject sizes that cannot hold the v1.1 fields +* read below, are not u64-aligned, or would overrun the +* x3..x17 window. +*/ +if (buf_sz <FFA_PART_INFO_DESC_V1_1_SZ || + buf_sz %sizeof (u64)) +return -EPROTO ; +if ((cur_idx - start_idx +1 ) * buf_sz > +FFA_PART_INFO_REGS_PAYLOAD_U64 *sizeof (u64)) +return -EPROTO ; regs = (void *)&partition_info.a3;for (idx =0 ; idx < cur_idx - start_idx +1 ; idx++, buf++) {
@@ -373,7 +402,7 @@ __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3, buf->exec_ctxt =PART_INFO_EXEC_CXT (val); buf->properties =PART_INFO_PROPERTIES (val);uuid_copy (&buf->uuid, &uuid_regs.uuid); - regs +=3 ; + regs += buf_sz /sizeof (u64); } prev_idx = cur_idx;
base-commit: 38edeaf4dcf3d8381ba801e494ba03179c145c70
Signature · 1 line
2.34.1
Sudeep Holla
maintainerQuoted 10 lines · Jamie Nguyen
__ffa_partition_info_get_regs()walks the response with a hardcoded 24-byte stride (regs += 3) even though the SPMC tells us the actual per-descriptor size viaPARTITION_INFO_SZin x2[63:48]. The size is read intobuf_szand then thrown away.That works while every SPMC returns the FF-A v1.1 layout, but it falls apart against a v1.3 SPMC returning the 48-byte descriptor. The loop strides over half a descriptor at a time and ends up parsing every other entry from a slice of two adjacent ones.
Quoted 9 lines
The FF-A spec (v1.2, section 18.5) says that the producer should report the descriptor size, and the consumer is supposed to stride by that size and ignore any trailing fields it doesn't understand. The non-REGS path (
__ffa_partition_info_get) does this already, and the REGS path should match.Use
buf_szfor the stride, and bail out with-EPROTOif the SPMC reports something we can't safely walk.
Can you check if the issue is addressed in -next by:
Commit 3974ea193840 ("firmware: arm_ffa: Bound PARTITION_INFO_GET_REGS copies")
Signature · 2 lines
Regards, Sudeep
Quoted 21 lines · Sudeep Holla
Quoted 16 lines · Jamie Nguyen
__ffa_partition_info_get_regs()walks the response with a hardcoded 24-byte stride (regs += 3) even though the SPMC tells us the actual per-descriptor size viaPARTITION_INFO_SZin x2[63:48]. The size is read intobuf_szand then thrown away.That works while every SPMC returns the FF-A v1.1 layout, but it falls apart against a v1.3 SPMC returning the 48-byte descriptor. The loop strides over half a descriptor at a time and ends up parsing every other entry from a slice of two adjacent ones.
The FF-A spec (v1.2, section 18.5) says that the producer should report the descriptor size, and the consumer is supposed to stride by that size and ignore any trailing fields it doesn't understand. The non-REGS path (
__ffa_partition_info_get) does this already, and the REGS path should match.
Quoted 6 lines
Quoted 3 lines
Use
buf_szfor the stride, and bail out with-EPROTOif the SPMC reports something we can't safely walk.Can you check if the issue is addressed in -next by:
Commit3974ea193840("firmware:arm_ffa: BoundPARTITION_INFO_GET_REGScopies")
Thanks for the pointer. I tested 3974ea193840 on the same hardware that reproduces the bug, but the descriptor-stride issue is still present.
The relevant loop at the end of __ffa_partition_info_get_regs() still has:
buf_sz = PARTITION_INFO_SZ(partition_info.a2);
if (buf_sz > sizeof(*buffer))
buf_sz = sizeof(*buffer);
...
for (idx = 0; idx < nr_desc; idx++, buf++) {
...
regs += 3; /* bug is here */
}With 48-byte descriptors the SPMC returns nr_desc = 2 per call, which passes the new MAX_DESC = 5 bound, and the loop then runs the 24-byte stride over 48-byte entries.
Here are the test results on the same SBIOS / same hardware:
unpatched: 5 partitions enumerated, two with
garbled UUIDs and partition_ids
3974ea193840 alone: same misparsed enumeration
3974ea193840 + my patch: 6 partitions, all entries correct
So the two fixes look complementary rather than overlapping.
Quoted 3 lines
Signature · 2 lines
Regards, Sudeep
Sudeep Holla
maintainerQuoted 44 lines · Jamie Nguyen
Quoted 21 lines · Sudeep Holla
Quoted 16 lines · Jamie Nguyen
__ffa_partition_info_get_regs()walks the response with a hardcoded 24-byte stride (regs += 3) even though the SPMC tells us the actual per-descriptor size viaPARTITION_INFO_SZin x2[63:48]. The size is read intobuf_szand then thrown away.That works while every SPMC returns the FF-A v1.1 layout, but it falls apart against a v1.3 SPMC returning the 48-byte descriptor. The loop strides over half a descriptor at a time and ends up parsing every other entry from a slice of two adjacent ones.
The FF-A spec (v1.2, section 18.5) says that the producer should report the descriptor size, and the consumer is supposed to stride by that size and ignore any trailing fields it doesn't understand. The non-REGS path (
__ffa_partition_info_get) does this already, and the REGS path should match.Quoted 6 lines
Quoted 3 lines
Use
buf_szfor the stride, and bail out with-EPROTOif the SPMC reports something we can't safely walk.Can you check if the issue is addressed in -next by:
Commit3974ea193840("firmware:arm_ffa: BoundPARTITION_INFO_GET_REGScopies")Thanks for the pointer. I tested
3974ea193840on the same hardware that reproduces the bug, but the descriptor-stride issue is still present.The relevant loop at the end of
__ffa_partition_info_get_regs()still has:buf_sz = PARTITION_INFO_SZ(partition_info.a2); if (buf_sz > sizeof(*buffer)) buf_sz = sizeof(*buffer); ... for (idx = 0; idx < nr_desc; idx++, buf++) { ... regs += 3; /* bug is here */ }
Quoted 1 line
With 48-byte descriptors the SPMC returns
nr_desc= 2 per call,
Well why is the firmware sending 48byte entry when 24byte is expected.
Signature · 2 lines
Regards, Sudeep
Quoted 49 lines · Sudeep Holla
Quoted 44 lines · Jamie Nguyen
Quoted 21 lines · Sudeep Holla
Quoted 16 lines · Jamie Nguyen
__ffa_partition_info_get_regs()walks the response with a hardcoded 24-byte stride (regs += 3) even though the SPMC tells us the actual per-descriptor size viaPARTITION_INFO_SZin x2[63:48]. The size is read intobuf_szand then thrown away.That works while every SPMC returns the FF-A v1.1 layout, but it falls apart against a v1.3 SPMC returning the 48-byte descriptor. The loop strides over half a descriptor at a time and ends up parsing every other entry from a slice of two adjacent ones.
The FF-A spec (v1.2, section 18.5) says that the producer should report the descriptor size, and the consumer is supposed to stride by that size and ignore any trailing fields it doesn't understand. The non-REGS path (
__ffa_partition_info_get) does this already, and the REGS path should match.Quoted 6 lines
Quoted 3 lines
Use
buf_szfor the stride, and bail out with-EPROTOif the SPMC reports something we can't safely walk.Can you check if the issue is addressed in -next by:
Commit3974ea193840("firmware:arm_ffa: BoundPARTITION_INFO_GET_REGScopies")Thanks for the pointer. I tested
3974ea193840on the same hardware that reproduces the bug, but the descriptor-stride issue is still present.The relevant loop at the end of
__ffa_partition_info_get_regs()still has:
buf_sz=PARTITION_INFO_SZ(partition_info.a2);
if (buf_sz>sizeof(*buffer))buf_sz=sizeof(*buffer);
...
for (idx = 0; idx <nr_desc; idx++, buf++) {
...
regs += 3; /* bug is here */
}
Quoted 3 lines
Quoted 1 line
With 48-byte descriptors the SPMC returns
nr_desc= 2 per call,Well why is the firmware sending 48byte entry when 24byte is expected.
We're starting to test with a v1.3 SPMC implementation. UEFI brings up its FF-A driver first and negotiates v1.3, which the SPMC then must keep for the lifetime of the primary VM per DEN0077A v1.2 REL0 section 13.2.2 [0]:
"Once an FF-A version has been negotiated between a caller and a
callee, the version may not be changed for the lifetime of the calling component.”
When Linux later asks for v1.2 the SPMC stays at 1.3 and emits the 48-byte v1.3 layout: DEN0077A v1.3 ALP4 section 13.9 [1]: "Size of each descriptor is 48 bytes as per the FF-A v1.3 spec”.
[0]: https://developer.arm.com/documentation/den0077/j
[1]: https://developer.arm.com/documentation/den0077/o
Quoted 3 lines
Signature · 2 lines
Regards, Sudeep
Sudeep Holla
maintainerQuoted 53 lines · Jamie Nguyen
Quoted 49 lines · Sudeep Holla
Quoted 44 lines · Jamie Nguyen
Quoted 21 lines · Sudeep Holla
Quoted 16 lines · Jamie Nguyen
__ffa_partition_info_get_regs()walks the response with a hardcoded 24-byte stride (regs += 3) even though the SPMC tells us the actual per-descriptor size viaPARTITION_INFO_SZin x2[63:48]. The size is read intobuf_szand then thrown away.That works while every SPMC returns the FF-A v1.1 layout, but it falls apart against a v1.3 SPMC returning the 48-byte descriptor. The loop strides over half a descriptor at a time and ends up parsing every other entry from a slice of two adjacent ones.
The FF-A spec (v1.2, section 18.5) says that the producer should report the descriptor size, and the consumer is supposed to stride by that size and ignore any trailing fields it doesn't understand. The non-REGS path (
__ffa_partition_info_get) does this already, and the REGS path should match.Quoted 6 lines
Quoted 3 lines
Use
buf_szfor the stride, and bail out with-EPROTOif the SPMC reports something we can't safely walk.Can you check if the issue is addressed in -next by:
Commit3974ea193840("firmware:arm_ffa: BoundPARTITION_INFO_GET_REGScopies")Thanks for the pointer. I tested
3974ea193840on the same hardware that reproduces the bug, but the descriptor-stride issue is still present.The relevant loop at the end of
__ffa_partition_info_get_regs()still has:
buf_sz=PARTITION_INFO_SZ(partition_info.a2);
if (buf_sz>sizeof(*buffer))buf_sz=sizeof(*buffer);
...
for (idx = 0; idx <nr_desc; idx++, buf++) {
...
regs += 3; /* bug is here */
}Quoted 3 lines
Quoted 1 line
With 48-byte descriptors the SPMC returns
nr_desc= 2 per call,Well why is the firmware sending 48byte entry when 24byte is expected.
Quoted 9 lines
We're starting to test with a v1.3 SPMC implementation. UEFI brings up its FF-A driver first and negotiates v1.3, which the SPMC then must keep for the lifetime of the primary VM per DEN0077A v1.2 REL0 section 13.2.2 [0]:
"Once an FF-A version has been negotiated between a caller and a
callee, the version may not be changed for the lifetime of the calling component.”
I need to check the v1.3-alpha specs to understand/recall the version negotiations but until v1.2 and the current driver which is v1.2 we don't have any idea about all these new changes. But since v1.2 already has size of descriptor, we can and must handle that independently of this new version (re-)negotiation.
Signature · 2 lines
Regards, Sudeep
Sudeep Holla
maintainerQuoted 37 lines · Jamie Nguyen
__ffa_partition_info_get_regs()walks the response with a hardcoded 24-byte stride (regs += 3) even though the SPMC tells us the actual per-descriptor size viaPARTITION_INFO_SZin x2[63:48]. The size is read intobuf_szand then thrown away.That works while every SPMC returns the FF-A v1.1 layout, but it falls apart against a v1.3 SPMC returning the 48-byte descriptor. The loop strides over half a descriptor at a time and ends up parsing every other entry from a slice of two adjacent ones.
The FF-A spec (v1.2, section 18.5) says that the producer should report the descriptor size, and the consumer is supposed to stride by that size and ignore any trailing fields it doesn't understand. The non-REGS path (
__ffa_partition_info_get) does this already, and the REGS path should match.Use
buf_szfor the stride, and bail out with-EPROTOif the SPMC reports something we can't safely walk.Fixes 7bc0f589c81d ("firmware: arm_ffa: Fix big-endian support in __ffa_partition_info_regs_get()")Signed-off-by Jamie Nguyen <[email protected]>--- drivers/firmware/arm_ffa/driver.c | 35 ++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-)drivers/firmware/arm_ffa/driver.c +4
-0@@ -321,6 +321,22 @@ __ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,#define PART_INFO_ID (x) ((u16)(FIELD_GET(PART_INFO_ID_MASK, (x))))#define PART_INFO_EXEC_CXT (x) ((u16)(FIELD_GET(PART_INFO_EXEC_CXT_MASK, (x))))#define PART_INFO_PROPERTIES (x) ((u32)(FIELD_GET(PART_INFO_PROPS_MASK, (x)))) + +/* +* FF-A v1.2 section 13.9 Table 13.40: registers x3..x17 carry the partition +* descriptors, i.e. 15 u64 of payload per FFA_PARTITION_INFO_GET_REGS call.
drivers/firmware/arm_ffa/driver.c __ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3, · line 337
Quoted 10 lines
drivers/firmware/arm_ffa/driver.c +10
-0+*/ +#define FFA_PART_INFO_REGS_PAYLOAD_U64 15 + +/* +* FF-A v1.1 partition information descriptor (FF-A v1.2 section 6.2.1 +* Table 6.1): id (2) + exec_ctxt (2) + properties (4) + UUID (16) = 24 +* bytes. This is the minimum size the SPMC must report; the kernel reads +* exactly these fields and ignores any trailing ones per the forward- +* compatibility rules in FF-A v1.2 section 18.5. +*/
I can't see any such details is the above mention version and section. Can you confirm you are looking at [1] ?
Signature · 4 lines
Regards, Sudeep [1] https://developer.arm.com/documentation/den0077/j
Quoted 47 lines · Sudeep Holla
Quoted 42 lines · Jamie Nguyen
__ffa_partition_info_get_regs()walks the response with a hardcoded 24-byte stride (regs += 3) even though the SPMC tells us the actual per-descriptor size viaPARTITION_INFO_SZin x2[63:48]. The size is read intobuf_szand then thrown away.That works while every SPMC returns the FF-A v1.1 layout, but it falls apart against a v1.3 SPMC returning the 48-byte descriptor. The loop strides over half a descriptor at a time and ends up parsing every other entry from a slice of two adjacent ones.
The FF-A spec (v1.2, section 18.5) says that the producer should report the descriptor size, and the consumer is supposed to stride by that size and ignore any trailing fields it doesn't understand. The non-REGS path (
__ffa_partition_info_get) does this already, and the REGS path should match.Use
buf_szfor the stride, and bail out with-EPROTOif the SPMC reports something we can't safely walk.Fixes 7bc0f589c81d ("firmware: arm_ffa: Fix big-endian support in __ffa_partition_info_regs_get()")Signed-off-by Jamie Nguyen <[email protected]>---
drivers/firmware/arm_ffa/driver.c| 35 ++++++++++++++++++++++++++++---1 file changed, 32 insertions(+), 3 deletions(-)drivers/firmware/arm_ffa/driver.c +9
-0@@ -321,6 +321,22 @@ __ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,#define PART_INFO_ID (x) ((u16)(FIELD_GET(PART_INFO_ID_MASK, (x))))#define PART_INFO_EXEC_CXT (x) ((u16)(FIELD_GET(PART_INFO_EXEC_CXT_MASK, (x))))#define PART_INFO_PROPERTIES (x) ((u32)(FIELD_GET(PART_INFO_PROPS_MASK, (x)))) + +/* +* FF-A v1.2 section 13.9 Table 13.40: registers x3..x17 carry the partition +* descriptors, i.e. 15 u64 of payload per FFA_PARTITION_INFO_GET_REGS call. +*/ +#define FFA_PART_INFO_REGS_PAYLOAD_U64 15 + +/* +* FF-A v1.1 partition information descriptor (FF-A v1.2 section 6.2.1
Quoted 8 lines
drivers/firmware/arm_ffa/driver.c __ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3, · line 337
Quoted 5 lines
drivers/firmware/arm_ffa/driver.c +5
-0+* Table 6.1): id (2) + exec_ctxt (2) + properties (4) + UUID (16) = 24 +* bytes. This is the minimum size the SPMC must report; the kernel reads +* exactly these fields and ignores any trailing ones per the forward- +* compatibility rules in FF-A v1.2 section 18.5. +*/ I can't see any such details is the above mention version and section. Can you confirm you are looking at [1] ?
Yes, I am looking at [1]. The size field and the rule are in two different places:
- Section 13.9 Table 13.40, page 188:
x2 Bits[63:48] = "Size in bytes of each partition information entry descriptor."
(read into buf_sz today and discarded)
- Section 18.5 page 264 rule 4:
"A consumer of this data structure uses the size corresponding
to the Framework version it implements to consume only fields defined in its version. Additional fields in the producer's version of this data structure are safely ignored enabling forward compatibility."
If you agree, I'll rebase against linux-next and send a v2.
[1]: https://developer.arm.com/documentation/den0077/j
Quoted 5 lines
Signature · 4 lines
Regards, Sudeep [1] https://developer.arm.com/documentation/den0077/j
Sudeep Holla
maintainerQuoted 56 lines · Jamie Nguyen
Quoted 47 lines · Sudeep Holla
Quoted 42 lines · Jamie Nguyen
__ffa_partition_info_get_regs()walks the response with a hardcoded 24-byte stride (regs += 3) even though the SPMC tells us the actual per-descriptor size viaPARTITION_INFO_SZin x2[63:48]. The size is read intobuf_szand then thrown away.That works while every SPMC returns the FF-A v1.1 layout, but it falls apart against a v1.3 SPMC returning the 48-byte descriptor. The loop strides over half a descriptor at a time and ends up parsing every other entry from a slice of two adjacent ones.
The FF-A spec (v1.2, section 18.5) says that the producer should report the descriptor size, and the consumer is supposed to stride by that size and ignore any trailing fields it doesn't understand. The non-REGS path (
__ffa_partition_info_get) does this already, and the REGS path should match.Use
buf_szfor the stride, and bail out with-EPROTOif the SPMC reports something we can't safely walk.Fixes 7bc0f589c81d ("firmware: arm_ffa: Fix big-endian support in __ffa_partition_info_regs_get()")Signed-off-by Jamie Nguyen <[email protected]>---
drivers/firmware/arm_ffa/driver.c| 35 ++++++++++++++++++++++++++++---1 file changed, 32 insertions(+), 3 deletions(-)drivers/firmware/arm_ffa/driver.c +9
-0@@ -321,6 +321,22 @@ __ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,#define PART_INFO_ID (x) ((u16)(FIELD_GET(PART_INFO_ID_MASK, (x))))#define PART_INFO_EXEC_CXT (x) ((u16)(FIELD_GET(PART_INFO_EXEC_CXT_MASK, (x))))#define PART_INFO_PROPERTIES (x) ((u32)(FIELD_GET(PART_INFO_PROPS_MASK, (x)))) + +/* +* FF-A v1.2 section 13.9 Table 13.40: registers x3..x17 carry the partition +* descriptors, i.e. 15 u64 of payload per FFA_PARTITION_INFO_GET_REGS call. +*/ +#define FFA_PART_INFO_REGS_PAYLOAD_U64 15 + +/* +* FF-A v1.1 partition information descriptor (FF-A v1.2 section 6.2.1 Quoted 8 lines
drivers/firmware/arm_ffa/driver.c __ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3, · line 337
Quoted 5 lines
drivers/firmware/arm_ffa/driver.c +5
-0+* Table 6.1): id (2) + exec_ctxt (2) + properties (4) + UUID (16) = 24 +* bytes. This is the minimum size the SPMC must report; the kernel reads +* exactly these fields and ignores any trailing ones per the forward- +* compatibility rules in FF-A v1.2 section 18.5. +*/ I can't see any such details is the above mention version and section. Can you confirm you are looking at [1] ?
Quoted 8 lines
Yes, I am looking at [1]. The size field and the rule are in two different places:
- Section 13.9 Table 13.40, page 188:
x2 Bits[63:48] = "Size in bytes of each partition information entry descriptor."
(read intobuf_sztoday and discarded)
Sorry my bad, I was looking at Section 18 and was confused a bit.
Quoted 7 lines
- Section 18.5 page 264 rule 4:
"A consumer of this data structure uses the size corresponding
to the Framework version it implements to consume only fields defined in its version. Additional fields in the producer's version of this data structure are safely ignored enabling forward compatibility."
Now makes sense, was just looking at this w/o reading Section 13.9
Quoted 2 lines
If you agree, I'll rebase against linux-next and send a v2.
Yes please. Thanks for your patience.
Signature · 2 lines
Regards, Sudeep
PATCH v2 1/1 firmware: arm_ffa: honor descriptor size in PARTITION_INFO_GET_REGS
__ffa_partition_info_get_regs() walks the response with a hardcoded 24-byte stride (regs += 3) even though the SPMC tells us the actual per-descriptor size via PARTITION_INFO_SZ in x2[63:48]. The size is read into buf_sz and then thrown away.
That works while every SPMC returns the FF-A v1.1 layout, but it falls apart against a v1.3 SPMC returning the 48-byte descriptor. The loop strides over half a descriptor at a time and ends up parsing every other entry from a slice of two adjacent ones.
The FF-A spec (v1.2, section 18.5) says that the producer should report the descriptor size, and the consumer is supposed to stride by that size and ignore any trailing fields it doesn't understand. The non-REGS path (__ffa_partition_info_get) does this already, and the REGS path should match.
Use buf_sz for the stride, and bail out with -EINVAL if the SPMC reports something we can't safely walk.
---
Changes in v2:
- Rebase onto linux-next; reuse the FFA_PART_INFO_GET_REGS_{REGS_PER_DESC,MAX_DESC} macros it added instead of introducing new ones.
- Return -EINVAL instead of -EPROTO to match surrounding checks.
- Update Fixes: tag to the commit that introduced the hardcoded stride.
--- drivers/firmware/arm_ffa/driver.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
drivers/firmware/arm_ffa/driver.c +17 -3
@@ -374,9 +374,23 @@ __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,return -EINVAL ; tag =UUID_INFO_TAG (partition_info.a2); + +/* +* Per FF-A v1.2 section 18.5 the SPMC reports the per- +* descriptor size and consumers must stride by that size, +* reading only the fields they understand and ignoring any +* trailing ones. Reject sizes that cannot hold the v1.1 +* fields parsed below, are not u64-aligned, or whose total +* payload would walk past the x3..x17 window (e.g. a v1.3 +* 48-byte descriptor with nr_desc > 2). +*/ buf_sz =PARTITION_INFO_SZ (partition_info.a2); -if (buf_sz >sizeof (*buffer)) - buf_sz =sizeof (*buffer); +if (buf_sz <FFA_PART_INFO_GET_REGS_REGS_PER_DESC *sizeof (u64) || + buf_sz %sizeof (u64) || + nr_desc * (buf_sz /sizeof (u64)) > +FFA_PART_INFO_GET_REGS_MAX_DESC * +FFA_PART_INFO_GET_REGS_REGS_PER_DESC ) +return -EINVAL ; regs = (void *)&partition_info.a3;for (idx =0 ; idx < nr_desc; idx++, buf++) {
@@ -395,7 +409,7 @@ __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3, buf->exec_ctxt =PART_INFO_EXEC_CXT (val); buf->properties =PART_INFO_PROPERTIES (val);uuid_copy (&buf->uuid, &uuid_regs.uuid); - regs +=3 ; + regs += buf_sz /sizeof (u64); } start_idx = cur_idx +1 ;
base-commit: e98d21c170b01ddef366f023bbfcf6b31509fa83
Signature · 1 line
2.34.1
Sudeep Holla
maintainerQuoted 40 lines · Jamie Nguyen
__ffa_partition_info_get_regs()walks the response with a hardcoded 24-byte stride (regs += 3) even though the SPMC tells us the actual per-descriptor size viaPARTITION_INFO_SZin x2[63:48]. The size is read intobuf_szand then thrown away.That works while every SPMC returns the FF-A v1.1 layout, but it falls apart against a v1.3 SPMC returning the 48-byte descriptor. The loop strides over half a descriptor at a time and ends up parsing every other entry from a slice of two adjacent ones.
The FF-A spec (v1.2, section 18.5) says that the producer should report the descriptor size, and the consumer is supposed to stride by that size and ignore any trailing fields it doesn't understand. The non-REGS path (
__ffa_partition_info_get) does this already, and the REGS path should match.Use
buf_szfor the stride, and bail out with-EINVALif the SPMC reports something we can't safely walk.Fixes ba85c644ac8d ("firmware: arm_ffa: Add support for FFA_PARTITION_INFO_GET_REGS")Signed-off-by Jamie Nguyen <[email protected]>---
Changes in v2:
- Rebase onto linux-next; reuse the FFA_PART_INFO_GET_REGS_{REGS_PER_DESC,MAX_DESC} macros it added instead of introducing new ones.
- Return-EINVALinstead of-EPROTOto match surrounding checks.
- Update Fixes: tag to the commit that introduced the hardcoded stride.--- drivers/firmware/arm_ffa/driver.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)drivers/firmware/arm_ffa/driver.c +0
-0@@ -374,9 +374,23 @@ __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,return -EINVAL ; tag =UUID_INFO_TAG (partition_info.a2);
drivers/firmware/arm_ffa/driver.c __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3, · line 386
Quoted 10 lines
drivers/firmware/arm_ffa/driver.c +10
-0+ +/* +* Per FF-A v1.2 section 18.5 the SPMC reports the per- +* descriptor size and consumers must stride by that size, +* reading only the fields they understand and ignoring any +* trailing ones. Reject sizes that cannot hold the v1.1 +* fields parsed below, are not u64-aligned, or whose total +* payload would walk past the x3..x17 window (e.g. a v1.3 +* 48-byte descriptor with nr_desc > 2). +*/
I am not sure if this above note is necessary.
drivers/firmware/arm_ffa/driver.c __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3, · line 394
Quoted 10 lines
drivers/firmware/arm_ffa/driver.c +6
-2buf_sz =PARTITION_INFO_SZ (partition_info.a2); -if (buf_sz >sizeof (*buffer)) - buf_sz =sizeof (*buffer); +if (buf_sz <FFA_PART_INFO_GET_REGS_REGS_PER_DESC *sizeof (u64) || + buf_sz %sizeof (u64) || + nr_desc * (buf_sz /sizeof (u64)) > +FFA_PART_INFO_GET_REGS_MAX_DESC * +FFA_PART_INFO_GET_REGS_REGS_PER_DESC ) +return -EINVAL ;
The logic above is correct but bit hard to read, I am thinking of splitting this
drivers/firmware/arm_ffa/driver.c __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3, · line 414
Quoted 10 lines
drivers/firmware/arm_ffa/driver.c +1
-1regs = (void *)&partition_info.a3;for (idx =0 ; idx < nr_desc; idx++, buf++) {@@ -395,7 +409,7 @@ __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3, buf->exec_ctxt =PART_INFO_EXEC_CXT (val); buf->properties =PART_INFO_PROPERTIES (val);uuid_copy (&buf->uuid, &uuid_regs.uuid); - regs +=3 ; + regs += buf_sz /sizeof (u64); } start_idx = cur_idx +1 ;
Is it OK if I adjust something like below patch ?
Regards,
Sudeep
-->8
drivers/firmware/arm_ffa/driver.c +16 -11
@@ -329,11 +329,9 @@ __ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,#define PART_INFO_EXEC_CXT_MASK GENMASK(31, 16)#define PART_INFO_PROPS_MASK GENMASK(63, 32)#define FFA_PART_INFO_GET_REGS_FIRST_REG 3 -#define FFA_PART_INFO_GET_REGS_REGS_PER_DESC 3 -#define FFA_PART_INFO_GET_REGS_MAX_DESC \ - (((sizeof(ffa_value_t) / sizeof_field(ffa_value_t, a0)) - \ - FFA_PART_INFO_GET_REGS_FIRST_REG) / \ - FFA_PART_INFO_GET_REGS_REGS_PER_DESC) +#define FFA_PART_INFO_GET_REGS_MIN_REGS_PER_DESC 3 +#define FFA_PART_INFO_GET_REGS_NUM_REGS \ + (sizeof(ffa_value_t) / sizeof_field(ffa_value_t, a0))#define PART_INFO_ID (x) ((u16)(FIELD_GET(PART_INFO_ID_MASK, (x))))#define PART_INFO_EXEC_CXT (x) ((u16)(FIELD_GET(PART_INFO_EXEC_CXT_MASK, (x))))#define PART_INFO_PROPERTIES (x) ((u32)(FIELD_GET(PART_INFO_PROPS_MASK, (x))))
@@ -347,7 +345,7 @@ __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3, do { __le64 *regs; - int idx, nr_desc, buf_idx; + int idx, nr_desc, buf_idx, regs_per_desc, max_desc; invoke_ffa_fn((ffa_value_t){ .a0 = FFA_PARTITION_INFO_GET_REGS,
@@ -370,8 +368,18 @@ __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,if (cur_idx < start_idx || cur_idx >= count)return -EINVAL ; + buf_sz =PARTITION_INFO_SZ (partition_info.a2); +if (buf_sz %sizeof (*regs)) +return -EINVAL ; + + regs_per_desc = buf_sz /sizeof (*regs); +if (regs_per_desc <FFA_PART_INFO_GET_REGS_MIN_REGS_PER_DESC ) +return -EINVAL ; + nr_desc = cur_idx - start_idx +1 ; -if (nr_desc >FFA_PART_INFO_GET_REGS_MAX_DESC ) + max_desc = (FFA_PART_INFO_GET_REGS_NUM_REGS - +FFA_PART_INFO_GET_REGS_FIRST_REG ) / regs_per_desc; +if (nr_desc > max_desc)return -EINVAL ; buf_idx = buf - buffer;
@@ -379,9 +387,6 @@ __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,return -EINVAL ; tag =UUID_INFO_TAG (partition_info.a2); - buf_sz =PARTITION_INFO_SZ (partition_info.a2); -if (buf_sz >sizeof (*buffer)) - buf_sz =sizeof (*buffer); regs = (void *)&partition_info.a3;for (idx =0 ; idx < nr_desc; idx++, buf++) {
@@ -400,7 +405,7 @@ __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3, buf->exec_ctxt =PART_INFO_EXEC_CXT (val); buf->properties =PART_INFO_PROPERTIES (val);uuid_copy (&buf->uuid, &uuid_regs.uuid); - regs +=3 ; + regs += regs_per_desc; } start_idx = cur_idx +1 ;
PATCH v3 1/1 firmware: arm_ffa: honor descriptor size in PARTITION_INFO_GET_REGS
__ffa_partition_info_get_regs() walks the response with a hardcoded 24-byte stride (regs += 3) even though the SPMC tells us the actual per-descriptor size via PARTITION_INFO_SZ in x2[63:48]. The size is read into buf_sz and then thrown away.
That works while every SPMC returns the FF-A v1.1 layout, but it falls apart against a v1.3 SPMC returning the 48-byte descriptor. The loop strides over half a descriptor at a time and ends up parsing every other entry from a slice of two adjacent ones.
The FF-A spec (v1.2, section 18.5) says that the producer should report the descriptor size, and the consumer is supposed to stride by that size and ignore any trailing fields it doesn't understand. The non-REGS path (__ffa_partition_info_get) does this already, and the REGS path should match.
Use buf_sz for the stride, and bail out with -EINVAL if the SPMC reports something we can't safely walk.
---
Changes in v3:
- Per Sudeep's review: drop the explanatory comment and split the buf_sz validation into three named checks (u64 alignment, minimum size for the v1.1 layout we parse, fit in the x3..x17 window for nr_desc).
- Replace FFA_PART_INFO_GET_REGS_REGS_PER_DESC with FFA_PART_INFO_GET_REGS_MIN_REGS_PER_DESC and replace FFA_PART_INFO_GET_REGS_MAX_DESC with FFA_PART_INFO_GET_REGS_NUM_REGS, computing max_desc per call from the SPMC-reported descriptor size.
- Drop the now-redundant if (buf_sz > sizeof(*buffer)) clamp.
Changes in v2:
- Rebase onto linux-next; reuse the FFA_PART_INFO_GET_REGS_{REGS_PER_DESC,MAX_DESC} macros it added instead of introducing new ones.
- Return -EINVAL instead of -EPROTO to match surrounding checks.
- Update Fixes: tag to the commit that introduced the hardcoded stride.
--- drivers/firmware/arm_ffa/driver.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-)
drivers/firmware/arm_ffa/driver.c +16 -11
@@ -324,11 +324,9 @@ __ffa_partition_info_get(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,#define PART_INFO_EXEC_CXT_MASK GENMASK(31, 16)#define PART_INFO_PROPS_MASK GENMASK(63, 32)#define FFA_PART_INFO_GET_REGS_FIRST_REG 3 -#define FFA_PART_INFO_GET_REGS_REGS_PER_DESC 3 -#define FFA_PART_INFO_GET_REGS_MAX_DESC \ - (((sizeof(ffa_value_t) / sizeof_field(ffa_value_t, a0)) - \ - FFA_PART_INFO_GET_REGS_FIRST_REG) / \ - FFA_PART_INFO_GET_REGS_REGS_PER_DESC) +#define FFA_PART_INFO_GET_REGS_MIN_REGS_PER_DESC 3 +#define FFA_PART_INFO_GET_REGS_NUM_REGS \ + (sizeof(ffa_value_t) / sizeof_field(ffa_value_t, a0))#define PART_INFO_ID (x) ((u16)(FIELD_GET(PART_INFO_ID_MASK, (x))))#define PART_INFO_EXEC_CXT (x) ((u16)(FIELD_GET(PART_INFO_EXEC_CXT_MASK, (x))))#define PART_INFO_PROPERTIES (x) ((u32)(FIELD_GET(PART_INFO_PROPS_MASK, (x))))
@@ -342,7 +340,7 @@ __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3, do { __le64 *regs; - int idx, nr_desc, buf_idx; + int idx, nr_desc, buf_idx, regs_per_desc, max_desc; invoke_ffa_fn((ffa_value_t){ .a0 = FFA_PARTITION_INFO_GET_REGS,
@@ -365,8 +363,18 @@ __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,if (cur_idx < start_idx || cur_idx >= count)return -EINVAL ; + buf_sz =PARTITION_INFO_SZ (partition_info.a2); +if (buf_sz %sizeof (*regs)) +return -EINVAL ; + + regs_per_desc = buf_sz /sizeof (*regs); +if (regs_per_desc <FFA_PART_INFO_GET_REGS_MIN_REGS_PER_DESC ) +return -EINVAL ; + nr_desc = cur_idx - start_idx +1 ; -if (nr_desc >FFA_PART_INFO_GET_REGS_MAX_DESC ) + max_desc = (FFA_PART_INFO_GET_REGS_NUM_REGS - +FFA_PART_INFO_GET_REGS_FIRST_REG ) / regs_per_desc; +if (nr_desc > max_desc)return -EINVAL ; buf_idx = buf - buffer;
@@ -374,9 +382,6 @@ __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3,return -EINVAL ; tag =UUID_INFO_TAG (partition_info.a2); - buf_sz =PARTITION_INFO_SZ (partition_info.a2); -if (buf_sz >sizeof (*buffer)) - buf_sz =sizeof (*buffer); regs = (void *)&partition_info.a3;for (idx =0 ; idx < nr_desc; idx++, buf++) {
@@ -395,7 +400,7 @@ __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3, buf->exec_ctxt =PART_INFO_EXEC_CXT (val); buf->properties =PART_INFO_PROPERTIES (val);uuid_copy (&buf->uuid, &uuid_regs.uuid); - regs +=3 ; + regs += regs_per_desc; } start_idx = cur_idx +1 ;
base-commit: e98d21c170b01ddef366f023bbfcf6b31509fa83
Signature · 1 line
2.34.1