Skip to content

Commit 0b9fc75

Browse files
Marc Zyngierscpcom
authored andcommitted
arm/arm64: smccc-1.1: Handle function result as parameters
[ Upstream commit 755a8bf ] If someone has the silly idea to write something along those lines: extern u64 foo(void); void bar(struct arm_smccc_res *res) { arm_smccc_1_1_smc(0xbad, foo(), res); } they are in for a surprise, as this gets compiled as: 0000000000000588 <bar>: 588: a9be7bfd stp x29, x30, [sp, #-32]! 58c: 910003fd mov x29, sp 590: f9000bf3 str x19, [sp, torvalds#16] 594: aa0003f3 mov x19, x0 598: aa1e03e0 mov x0, x30 59c: 94000000 bl 0 <_mcount> 5a0: 94000000 bl 0 <foo> 5a4: aa0003e1 mov x1, x0 5a8: d4000003 smc #0x0 5ac: b4000073 cbz x19, 5b8 <bar+0x30> 5b0: a9000660 stp x0, x1, [x19] 5b4: a9010e62 stp x2, x3, [x19, torvalds#16] 5b8: f9400bf3 ldr x19, [sp, torvalds#16] 5bc: a8c27bfd ldp x29, x30, [sp], torvalds#32 5c0: d65f03c0 ret 5c4: d503201f nop The call to foo "overwrites" the x0 register for the return value, and we end up calling the wrong secure service. A solution is to evaluate all the parameters before assigning anything to specific registers, leading to the expected result: 0000000000000588 <bar>: 588: a9be7bfd stp x29, x30, [sp, #-32]! 58c: 910003fd mov x29, sp 590: f9000bf3 str x19, [sp, torvalds#16] 594: aa0003f3 mov x19, x0 598: aa1e03e0 mov x0, x30 59c: 94000000 bl 0 <_mcount> 5a0: 94000000 bl 0 <foo> 5a4: aa0003e1 mov x1, x0 5a8: d28175a0 mov x0, #0xbad 5ac: d4000003 smc #0x0 5b0: b4000073 cbz x19, 5bc <bar+0x34> 5b4: a9000660 stp x0, x1, [x19] 5b8: a9010e62 stp x2, x3, [x19, torvalds#16] 5bc: f9400bf3 ldr x19, [sp, torvalds#16] 5c0: a8c27bfd ldp x29, x30, [sp], torvalds#32 5c4: d65f03c0 ret Reported-by: Julien Grall <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Signed-off-by: Will Deacon <[email protected]> Signed-off-by: Sasha Levin <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Ard Biesheuvel <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> mainline 8ca266d
1 parent bcb7fed commit 0b9fc75

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

include/linux/arm-smccc.h

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,41 +173,51 @@ asmlinkage void arm_smccc_hvc(unsigned long a0, unsigned long a1,
173173
register unsigned long r3 asm("r3")
174174

175175
#define __declare_arg_1(a0, a1, res) \
176+
typeof(a1) __a1 = a1; \
176177
struct arm_smccc_res *___res = res; \
177178
register unsigned long r0 asm("r0") = (u32)a0; \
178-
register unsigned long r1 asm("r1") = a1; \
179+
register unsigned long r1 asm("r1") = __a1; \
179180
register unsigned long r2 asm("r2"); \
180181
register unsigned long r3 asm("r3")
181182

182183
#define __declare_arg_2(a0, a1, a2, res) \
184+
typeof(a1) __a1 = a1; \
185+
typeof(a2) __a2 = a2; \
183186
struct arm_smccc_res *___res = res; \
184187
register unsigned long r0 asm("r0") = (u32)a0; \
185-
register unsigned long r1 asm("r1") = a1; \
186-
register unsigned long r2 asm("r2") = a2; \
188+
register unsigned long r1 asm("r1") = __a1; \
189+
register unsigned long r2 asm("r2") = __a2; \
187190
register unsigned long r3 asm("r3")
188191

189192
#define __declare_arg_3(a0, a1, a2, a3, res) \
193+
typeof(a1) __a1 = a1; \
194+
typeof(a2) __a2 = a2; \
195+
typeof(a3) __a3 = a3; \
190196
struct arm_smccc_res *___res = res; \
191197
register unsigned long r0 asm("r0") = (u32)a0; \
192-
register unsigned long r1 asm("r1") = a1; \
193-
register unsigned long r2 asm("r2") = a2; \
194-
register unsigned long r3 asm("r3") = a3
198+
register unsigned long r1 asm("r1") = __a1; \
199+
register unsigned long r2 asm("r2") = __a2; \
200+
register unsigned long r3 asm("r3") = __a3
195201

196202
#define __declare_arg_4(a0, a1, a2, a3, a4, res) \
203+
typeof(a4) __a4 = a4; \
197204
__declare_arg_3(a0, a1, a2, a3, res); \
198-
register typeof(a4) r4 asm("r4") = a4
205+
register unsigned long r4 asm("r4") = __a4
199206

200207
#define __declare_arg_5(a0, a1, a2, a3, a4, a5, res) \
208+
typeof(a5) __a5 = a5; \
201209
__declare_arg_4(a0, a1, a2, a3, a4, res); \
202-
register typeof(a5) r5 asm("r5") = a5
210+
register unsigned long r5 asm("r5") = __a5
203211

204212
#define __declare_arg_6(a0, a1, a2, a3, a4, a5, a6, res) \
213+
typeof(a6) __a6 = a6; \
205214
__declare_arg_5(a0, a1, a2, a3, a4, a5, res); \
206-
register typeof(a6) r6 asm("r6") = a6
215+
register unsigned long r6 asm("r6") = __a6
207216

208217
#define __declare_arg_7(a0, a1, a2, a3, a4, a5, a6, a7, res) \
218+
typeof(a7) __a7 = a7; \
209219
__declare_arg_6(a0, a1, a2, a3, a4, a5, a6, res); \
210-
register typeof(a7) r7 asm("r7") = a7
220+
register unsigned long r7 asm("r7") = __a7
211221

212222
#define ___declare_args(count, ...) __declare_arg_ ## count(__VA_ARGS__)
213223
#define __declare_args(count, ...) ___declare_args(count, __VA_ARGS__)

0 commit comments

Comments
 (0)