mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 02:30:34 +02:00 
			
		
		
		
	fprobe: make fprobe_kprobe_handler recursion free
Current implementation calls kprobe related functions before doing
ftrace recursion check in fprobe_kprobe_handler, which opens door
to kernel crash due to stack recursion if preempt_count_{add, sub}
is traceable in kprobe_busy_{begin, end}.
Things goes like this without this patch quoted from Steven:
"
fprobe_kprobe_handler() {
   kprobe_busy_begin() {
      preempt_disable() {
         preempt_count_add() {  <-- trace
            fprobe_kprobe_handler() {
		[ wash, rinse, repeat, CRASH!!! ]
"
By refactoring the common part out of fprobe_kprobe_handler and
fprobe_handler and call ftrace recursion detection at the very beginning,
the whole fprobe_kprobe_handler is free from recursion.
[ Fix the indentation of __fprobe_handler() parameters. ]
Link: https://lore.kernel.org/all/20230517034510.15639-3-zegao@tencent.com/
Fixes: ab51e15d53 ("fprobe: Introduce FPROBE_FL_KPROBE_SHARED flag for fprobe")
Signed-off-by: Ze Gao <zegao@tencent.com>
Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: stable@vger.kernel.org
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
			
			
This commit is contained in:
		
							parent
							
								
									be243bacfb
								
							
						
					
					
						commit
						3cc4e2c5fb
					
				
					 1 changed files with 44 additions and 15 deletions
				
			
		| 
						 | 
					@ -20,30 +20,22 @@ struct fprobe_rethook_node {
 | 
				
			||||||
	char data[];
 | 
						char data[];
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void fprobe_handler(unsigned long ip, unsigned long parent_ip,
 | 
					static inline void __fprobe_handler(unsigned long ip, unsigned long parent_ip,
 | 
				
			||||||
			   struct ftrace_ops *ops, struct ftrace_regs *fregs)
 | 
								struct ftrace_ops *ops, struct ftrace_regs *fregs)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct fprobe_rethook_node *fpr;
 | 
						struct fprobe_rethook_node *fpr;
 | 
				
			||||||
	struct rethook_node *rh = NULL;
 | 
						struct rethook_node *rh = NULL;
 | 
				
			||||||
	struct fprobe *fp;
 | 
						struct fprobe *fp;
 | 
				
			||||||
	void *entry_data = NULL;
 | 
						void *entry_data = NULL;
 | 
				
			||||||
	int bit, ret = 0;
 | 
						int ret = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	fp = container_of(ops, struct fprobe, ops);
 | 
						fp = container_of(ops, struct fprobe, ops);
 | 
				
			||||||
	if (fprobe_disabled(fp))
 | 
					 | 
				
			||||||
		return;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	bit = ftrace_test_recursion_trylock(ip, parent_ip);
 | 
					 | 
				
			||||||
	if (bit < 0) {
 | 
					 | 
				
			||||||
		fp->nmissed++;
 | 
					 | 
				
			||||||
		return;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (fp->exit_handler) {
 | 
						if (fp->exit_handler) {
 | 
				
			||||||
		rh = rethook_try_get(fp->rethook);
 | 
							rh = rethook_try_get(fp->rethook);
 | 
				
			||||||
		if (!rh) {
 | 
							if (!rh) {
 | 
				
			||||||
			fp->nmissed++;
 | 
								fp->nmissed++;
 | 
				
			||||||
			goto out;
 | 
								return;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		fpr = container_of(rh, struct fprobe_rethook_node, node);
 | 
							fpr = container_of(rh, struct fprobe_rethook_node, node);
 | 
				
			||||||
		fpr->entry_ip = ip;
 | 
							fpr->entry_ip = ip;
 | 
				
			||||||
| 
						 | 
					@ -61,23 +53,60 @@ static void fprobe_handler(unsigned long ip, unsigned long parent_ip,
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
			rethook_hook(rh, ftrace_get_regs(fregs), true);
 | 
								rethook_hook(rh, ftrace_get_regs(fregs), true);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
out:
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void fprobe_handler(unsigned long ip, unsigned long parent_ip,
 | 
				
			||||||
 | 
							struct ftrace_ops *ops, struct ftrace_regs *fregs)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct fprobe *fp;
 | 
				
			||||||
 | 
						int bit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fp = container_of(ops, struct fprobe, ops);
 | 
				
			||||||
 | 
						if (fprobe_disabled(fp))
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* recursion detection has to go before any traceable function and
 | 
				
			||||||
 | 
						 * all functions before this point should be marked as notrace
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						bit = ftrace_test_recursion_trylock(ip, parent_ip);
 | 
				
			||||||
 | 
						if (bit < 0) {
 | 
				
			||||||
 | 
							fp->nmissed++;
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						__fprobe_handler(ip, parent_ip, ops, fregs);
 | 
				
			||||||
	ftrace_test_recursion_unlock(bit);
 | 
						ftrace_test_recursion_unlock(bit);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
NOKPROBE_SYMBOL(fprobe_handler);
 | 
					NOKPROBE_SYMBOL(fprobe_handler);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void fprobe_kprobe_handler(unsigned long ip, unsigned long parent_ip,
 | 
					static void fprobe_kprobe_handler(unsigned long ip, unsigned long parent_ip,
 | 
				
			||||||
				  struct ftrace_ops *ops, struct ftrace_regs *fregs)
 | 
									  struct ftrace_ops *ops, struct ftrace_regs *fregs)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct fprobe *fp = container_of(ops, struct fprobe, ops);
 | 
						struct fprobe *fp;
 | 
				
			||||||
 | 
						int bit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fp = container_of(ops, struct fprobe, ops);
 | 
				
			||||||
 | 
						if (fprobe_disabled(fp))
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* recursion detection has to go before any traceable function and
 | 
				
			||||||
 | 
						 * all functions called before this point should be marked as notrace
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						bit = ftrace_test_recursion_trylock(ip, parent_ip);
 | 
				
			||||||
 | 
						if (bit < 0) {
 | 
				
			||||||
 | 
							fp->nmissed++;
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (unlikely(kprobe_running())) {
 | 
						if (unlikely(kprobe_running())) {
 | 
				
			||||||
		fp->nmissed++;
 | 
							fp->nmissed++;
 | 
				
			||||||
		return;
 | 
							return;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	kprobe_busy_begin();
 | 
						kprobe_busy_begin();
 | 
				
			||||||
	fprobe_handler(ip, parent_ip, ops, fregs);
 | 
						__fprobe_handler(ip, parent_ip, ops, fregs);
 | 
				
			||||||
	kprobe_busy_end();
 | 
						kprobe_busy_end();
 | 
				
			||||||
 | 
						ftrace_test_recursion_unlock(bit);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void fprobe_exit_handler(struct rethook_node *rh, void *data,
 | 
					static void fprobe_exit_handler(struct rethook_node *rh, void *data,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue