ibm,open-errinjct returns the session token in rets[0] and the status in rets[1], unlike other RTAS functions, which return the status in rets[0]. rtas_call() would report the session token as the status and drop the real status. Take the status from rets[1] for this function and return the session token in outputs[0]. This needs exactly two return cells, so reject any other count from kernel callers with -EINVAL and a one-time warning, and document the exception in the rtas_call() kernel-doc. Give the rtas_output tracepoint the index of the status cell, so that it reports rets[1] as the status and the session token as an additional output for this function. A call with no return cells reports status 0 instead of reading a cell that does not exist. Ensure __fetch_rtas_last_error() and sys_rtas() initialize rtas_args.rets to point into rtas_args.args[] after copying local arguments, preventing tracepoint handlers from dereferencing uninitialized memory or reading stale on-stack values. sys_rtas() still copies the raw return cells to userspace. It uses the status from the right cell for hardware-error (-1) detection and, after the lockdown check, rejects fewer than two return cells for this function. Link: https://files.openpower.foundation/s/XFgfMaqLMD5Bcm8 Signed-off-by: Narayana Murty N --- arch/powerpc/include/asm/trace.h | 26 +++++++--- arch/powerpc/kernel/rtas.c | 83 ++++++++++++++++++++++++++------ 2 files changed, 88 insertions(+), 21 deletions(-) diff --git a/arch/powerpc/include/asm/trace.h b/arch/powerpc/include/asm/trace.h index a7b69b25296b..d731f70aead7 100644 --- a/arch/powerpc/include/asm/trace.h +++ b/arch/powerpc/include/asm/trace.h @@ -146,25 +146,37 @@ TRACE_EVENT(rtas_input, ) ); +/* + * @status_idx is the index in rets[] of the status cell: 1 for + * ibm,open-errinjct, which returns its session token in rets[0], and 0 for + * every other function. The remaining cells are reported as other outputs. + */ TRACE_EVENT(rtas_output, - TP_PROTO(struct rtas_args *rtas_args, const char *name), + TP_PROTO(struct rtas_args *rtas_args, const char *name, + unsigned int status_idx), - TP_ARGS(rtas_args, name), + TP_ARGS(rtas_args, name, status_idx), TP_STRUCT__entry( __field(__u32, nr_other) __field(__s32, status) __string(name, name) - __dynamic_array(__u32, other_outputs, be32_to_cpu(rtas_args->nret) - 1) + __dynamic_array(__u32, other_outputs, + be32_to_cpu(rtas_args->nret) ? + be32_to_cpu(rtas_args->nret) - 1 : 0) ), TP_fast_assign( - __entry->nr_other = be32_to_cpu(rtas_args->nret) - 1; - __entry->status = be32_to_cpu(rtas_args->rets[0]); + u32 nret = be32_to_cpu(rtas_args->nret); + u32 *other = __get_dynamic_array(other_outputs); + u32 i; + + __entry->nr_other = nret ? nret - 1 : 0; + __entry->status = nret ? be32_to_cpu(rtas_args->rets[status_idx]) : 0; __assign_str(name); - be32_to_cpu_array(__get_dynamic_array(other_outputs), - &rtas_args->rets[1], __entry->nr_other); + for (i = 0; i < __entry->nr_other; i++) + other[i] = be32_to_cpu(rtas_args->rets[i < status_idx ? i : i + 1]); ), TP_printk("%s status: %d, other outputs: %s", __get_str(name), __entry->status, diff --git a/arch/powerpc/kernel/rtas.c b/arch/powerpc/kernel/rtas.c index 8d81c1e7a8db..201be33d9bd3 100644 --- a/arch/powerpc/kernel/rtas.c +++ b/arch/powerpc/kernel/rtas.c @@ -693,6 +693,32 @@ static const struct rtas_function *rtas_token_to_function(s32 token) /* This is here deliberately so it's only used in this file */ void enter_rtas(unsigned long); +static inline bool rtas_token_is_open_errinjct(s32 token) +{ + return token == rtas_function_token(RTAS_FN_IBM_OPEN_ERRINJCT); +} + +/* + * Return the index of the status cell in rets[]. ibm,open-errinjct returns + * the session token in rets[0] and the status in rets[1]. Every other RTAS + * function returns the status in rets[0]. + */ +static unsigned int rtas_status_idx(s32 token, u32 nret) +{ + return (nret >= 2 && rtas_token_is_open_errinjct(token)) ? 1 : 0; +} + +/* Return the status of a completed call, or 0 if it has no return cells. */ +static int rtas_status_from_args(s32 token, struct rtas_args *args) +{ + u32 nret = be32_to_cpu(args->nret); + + if (!nret) + return 0; + + return be32_to_cpu(args->rets[rtas_status_idx(token, nret)]); +} + static void __do_enter_rtas(struct rtas_args *args) { enter_rtas(__pa(args)); @@ -719,7 +745,9 @@ static void __do_enter_rtas_trace(struct rtas_args *args) __do_enter_rtas(args); trace_rtas_ll_exit(args); - trace_rtas_output(args, func->name); + trace_rtas_output(args, func->name, + rtas_status_idx(be32_to_cpu(args->token), + be32_to_cpu(args->nret))); } static void do_enter_rtas(struct rtas_args *args) @@ -1034,6 +1062,7 @@ static char *__fetch_rtas_last_error(char *altbuf) save_args = rtas_args; rtas_args = err_args; + rtas_args.rets = &rtas_args.args[2]; do_enter_rtas(&rtas_args); @@ -1122,20 +1151,28 @@ static bool token_is_restricted_errinjct(s32 token) * @token: Identifies the function being invoked. * @nargs: Number of input parameters. Does not include token. * @nret: Number of output parameters, including the call status. - * @outputs: Array of @nret output words. + * @outputs: Array of @nret - 1 output words, or NULL. * @....: List of @nargs input parameters. * * Invokes the RTAS function indicated by @token, which the caller * should obtain via rtas_function_token(). * * The @nargs and @nret arguments must match the number of input and - * output parameters specified for the RTAS function. + * output parameters specified for the RTAS function. @nret includes the + * status cell, so @outputs normally receives the remaining @nret - 1 + * return cells. * - * rtas_call() returns RTAS status codes, not conventional Linux errno - * values. Callers must translate any failure to an appropriate errno - * in syscall context. Most callers of RTAS functions that can return - * -2 or 990x should use rtas_busy_delay() to correctly handle those - * statuses before calling again. + * ibm,open-errinjct is the exception: it returns its status in rets[1] + * and a session token in rets[0]. rtas_call() still returns the status, + * and @outputs[0] receives the session token. @nret must be 2 for this + * function; any other value fails with -EINVAL, a Linux errno rather + * than an RTAS status, without calling RTAS. + * + * Apart from that case, rtas_call() returns RTAS status codes, not + * conventional Linux errno values. Callers must translate any failure to + * an appropriate errno in syscall context. Most callers of RTAS functions + * that can return -2 or 990x should use rtas_busy_delay() to correctly + * handle those statuses before calling again. * * The return value descriptions are adapted from 7.2.8 [RTAS] Return * Codes of the PAPR and CHRP specifications. @@ -1198,6 +1235,12 @@ int rtas_call(int token, int nargs, int nret, int *outputs, ...) return -1; } + if (rtas_token_is_open_errinjct(token) && nret != 2) { + WARN_ONCE(1, "rtas: ibm,open-errinjct requires exactly two return cells (got %d)\n", + nret); + return -EINVAL; + } + if ((mfmsr() & (MSR_IR|MSR_DR)) != (MSR_IR|MSR_DR)) { WARN_ON_ONCE(1); return -1; @@ -1215,13 +1258,20 @@ int rtas_call(int token, int nargs, int nret, int *outputs, ...) /* A -1 return code indicates that the last command couldn't be completed due to a hardware error. */ - if (be32_to_cpu(args->rets[0]) == -1) + if (rtas_status_from_args(token, args) == -1) buff_copy = __fetch_rtas_last_error(NULL); - if (nret > 1 && outputs != NULL) - for (i = 0; i < nret-1; ++i) - outputs[i] = be32_to_cpu(args->rets[i + 1]); - ret = (nret > 0) ? be32_to_cpu(args->rets[0]) : 0; + if (rtas_token_is_open_errinjct(token)) { + /* nret is 2: rets[0] is the session token, rets[1] the status. */ + if (outputs) + outputs[0] = be32_to_cpu(args->rets[0]); + ret = be32_to_cpu(args->rets[1]); + } else { + if (nret > 1 && outputs != NULL) + for (i = 0; i < nret-1; ++i) + outputs[i] = be32_to_cpu(args->rets[i + 1]); + ret = (nret > 0) ? be32_to_cpu(args->rets[0]) : 0; + } lockdep_unpin_lock(&rtas_lock, cookie); raw_spin_unlock_irqrestore(&rtas_lock, flags); @@ -1904,6 +1954,10 @@ SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs) return err; } + /* ibm,open-errinjct needs rets[1] for its status. */ + if (rtas_token_is_open_errinjct(token) && nret < 2) + return -EINVAL; + /* Need to handle ibm,suspend_me call specially */ if (token == rtas_function_token(RTAS_FN_IBM_SUSPEND_ME)) { @@ -1939,12 +1993,13 @@ SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs) cookie = lockdep_pin_lock(&rtas_lock); rtas_args = args; + rtas_args.rets = &rtas_args.args[nargs]; do_enter_rtas(&rtas_args); args = rtas_args; /* A -1 return code indicates that the last command couldn't be completed due to a hardware error. */ - if (be32_to_cpu(args.rets[0]) == -1) + if (rtas_status_from_args(token, &args) == -1) errbuf = __fetch_rtas_last_error(buff_copy); lockdep_unpin_lock(&rtas_lock, cookie); -- 2.54.0