Skip to content

Commit fab9ced

Browse files
softwareckifabiobaltieri
authored andcommitted
logging: sensing: tests: Add missing compiler barriers
Add missing memory barriers after branching on k_is_user_context() to prevent reordering possible of privileged memory access. Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
1 parent d28b900 commit fab9ced

File tree

8 files changed

+17
-2
lines changed

8 files changed

+17
-2
lines changed

include/zephyr/arch/xtensa/arch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ void xtensa_arch_kernel_oops(int reason_p, void *ssf);
111111
arch_syscall_invoke1(reason_p, \
112112
K_SYSCALL_XTENSA_USER_FAULT); \
113113
} else { \
114+
compiler_barrier(); \
114115
xtensa_arch_except(reason_p); \
115116
} \
116117
CODE_UNREACHABLE; \

include/zephyr/logging/log_msg.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,10 @@ do { \
531531
( \
532532
bool can_simple = LOG_MSG_SIMPLE_CHECK(__VA_ARGS__); \
533533
if (can_simple && ((_dlen) == 0) && !k_is_user_context()) { \
534-
LOG_MSG_DBG("create fast message\n");\
534+
compiler_barrier(); \
535+
LOG_MSG_DBG("create fast message\n"); \
535536
Z_LOG_MSG_SIMPLE_ARGS_CREATE(_domain_id, _source, _level, \
536-
Z_LOG_FMT_ARGS(_fmt, ##__VA_ARGS__)); \
537+
Z_LOG_FMT_ARGS(_fmt, ##__VA_ARGS__)); \
537538
_mode = Z_LOG_MSG_MODE_SIMPLE; \
538539
break; \
539540
} \

include/zephyr/syscall.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ static ALWAYS_INLINE bool z_syscall_trap(void)
110110
* Indicate whether the CPU is currently in user mode
111111
*
112112
* @return true if the CPU is currently running with user permissions
113+
*
114+
* CAUTION!
115+
* If you branch on k_is_user_context() and then perform a kernel-only operation, you must insert
116+
* a memory barrier before the privileged operation. Both the compiler and the CPU may reorder
117+
* memory operations around the branch. Without a barrier, memory accesses related to the privileged
118+
* path may move before the context check or be speculated.
113119
*/
114120
__pinned_func
115121
static inline bool k_is_user_context(void)

lib/os/printk.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ void vprintk(const char *fmt, va_list ap)
123123
buf_flush(&ctx);
124124
}
125125
} else {
126+
compiler_barrier();
126127
#ifdef CONFIG_PRINTK_SYNC
127128
k_spinlock_key_t key = k_spin_lock(&lock);
128129
#endif

subsys/logging/log_msg.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,13 +376,15 @@ void z_log_msg_runtime_vcreate(uint8_t domain_id, const void *source,
376376
pkg = alloca(plen);
377377
msg = NULL;
378378
} else if (IS_ENABLED(CONFIG_LOG_MODE_DEFERRED) && BACKENDS_IN_USE()) {
379+
compiler_barrier();
379380
msg = z_log_msg_alloc(msg_wlen);
380381
if (IS_ENABLED(CONFIG_LOG_FRONTEND) && msg == NULL) {
381382
pkg = alloca(plen);
382383
} else {
383384
pkg = msg ? msg->data : NULL;
384385
}
385386
} else {
387+
compiler_barrier();
386388
msg = alloca(msg_wlen * sizeof(int));
387389
pkg = msg->data;
388390
}
@@ -395,6 +397,7 @@ void z_log_msg_runtime_vcreate(uint8_t domain_id, const void *source,
395397
if (k_is_user_context()) {
396398
z_log_msg_static_create(source, desc, pkg, data);
397399
} else {
400+
compiler_barrier();
398401
if (IS_ENABLED(CONFIG_LOG_FRONTEND) &&
399402
frontend_runtime_filtering(source, desc.level)) {
400403
log_frontend_msg(source, desc, pkg, data);

subsys/sensing/dispatch.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ static void dispatch_task(void *a, void *b, void *c)
8686
ARG_UNUSED(c);
8787

8888
if (IS_ENABLED(CONFIG_USERSPACE) && !k_is_user_context()) {
89+
compiler_barrier();
8990
rtio_access_grant(&sensing_rtio_ctx, k_current_get());
9091
k_thread_user_mode_enter(dispatch_task, a, b, c);
9192
}

tests/drivers/counter/counter_basic_api/src/test_counter.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ static void counter_setup_instance(const struct device *dev)
230230
{
231231
k_sem_reset(&alarm_cnt_sem);
232232
if (!k_is_user_context()) {
233+
compiler_barrier();
233234
alarm_cnt = 0;
234235
}
235236
}

tests/net/socket/udp/src/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,6 +1847,7 @@ static void run_ancillary_recvmsg_test(int client_sock,
18471847
}
18481848

18491849
if (!k_is_user_context()) {
1850+
compiler_barrier();
18501851
iface = net_if_get_default();
18511852
zassert_equal(ifindex, net_if_get_by_iface(iface));
18521853
}

0 commit comments

Comments
 (0)