1+ #include <linux/rcupdate.h>
2+ #include <linux/slab.h>
3+ #include <linux/task_work.h>
14#include "manager.h"
25#include <asm/current.h>
36#include <linux/compat.h>
@@ -149,10 +152,18 @@ static int __maybe_unused count(struct user_arg_ptr argv, int max)
149152 return i ;
150153}
151154
155+ static void on_post_fs_data_cbfun (struct callback_head * cb )
156+ {
157+ on_post_fs_data ();
158+ }
159+
160+ static struct callback_head on_post_fs_data_cb = { .func =
161+ on_post_fs_data_cbfun };
162+
152163// IMPORTANT NOTE: the call from execve_handler_pre WON'T provided correct value for envp and flags in GKI version
153164int ksu_handle_execveat_ksud (int * fd , struct filename * * filename_ptr ,
154- struct user_arg_ptr * argv ,
155- struct user_arg_ptr * envp , int * flags )
165+ struct user_arg_ptr * argv ,
166+ struct user_arg_ptr * envp , int * flags )
156167{
157168#ifndef CONFIG_KPROBES
158169 if (!ksu_execveat_hook ) {
@@ -179,19 +190,17 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
179190 }
180191
181192 if (unlikely (!memcmp (filename -> name , system_bin_init ,
182- sizeof (system_bin_init ) - 1 ) &&
183- argv )) {
193+ sizeof (system_bin_init ) - 1 ) &&
194+ argv )) {
184195 // /system/bin/init executed
185196 int argc = count (* argv , MAX_ARG_STRINGS );
186197 pr_info ("/system/bin/init argc: %d\n" , argc );
187198 if (argc > 1 && !init_second_stage_executed ) {
188199 const char __user * p = get_user_arg_ptr (* argv , 1 );
189200 if (p && !IS_ERR (p )) {
190201 char first_arg [16 ];
191- ksu_strncpy_from_user_nofault (
192- first_arg , p , sizeof (first_arg ));
193- pr_info ("/system/bin/init first arg: %s\n" ,
194- first_arg );
202+ ksu_strncpy_from_user_nofault (first_arg , p , sizeof (first_arg ));
203+ pr_info ("/system/bin/init first arg: %s\n" , first_arg );
195204 if (!strcmp (first_arg , "second_stage" )) {
196205 pr_info ("/system/bin/init second_stage executed\n" );
197206 apply_kernelsu_rules ();
@@ -203,8 +212,8 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
203212 }
204213 }
205214 } else if (unlikely (!memcmp (filename -> name , old_system_init ,
206- sizeof (old_system_init ) - 1 ) &&
207- argv )) {
215+ sizeof (old_system_init ) - 1 ) &&
216+ argv )) {
208217 // /init executed
209218 int argc = count (* argv , MAX_ARG_STRINGS );
210219 pr_info ("/init argc: %d\n" , argc );
@@ -213,8 +222,7 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
213222 const char __user * p = get_user_arg_ptr (* argv , 1 );
214223 if (p && !IS_ERR (p )) {
215224 char first_arg [16 ];
216- ksu_strncpy_from_user_nofault (
217- first_arg , p , sizeof (first_arg ));
225+ ksu_strncpy_from_user_nofault (first_arg , p , sizeof (first_arg ));
218226 pr_info ("/init first arg: %s\n" , first_arg );
219227 if (!strcmp (first_arg , "--second-stage" )) {
220228 pr_info ("/init second_stage executed\n" );
@@ -231,15 +239,13 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
231239 if (envc > 0 ) {
232240 int n ;
233241 for (n = 1 ; n <= envc ; n ++ ) {
234- const char __user * p =
235- get_user_arg_ptr (* envp , n );
242+ const char __user * p = get_user_arg_ptr (* envp , n );
236243 if (!p || IS_ERR (p )) {
237244 continue ;
238245 }
239246 char env [256 ];
240247 // Reading environment variable strings from user space
241- if (ksu_strncpy_from_user_nofault (
242- env , p , sizeof (env )) < 0 )
248+ if (ksu_strncpy_from_user_nofault (env , p , sizeof (env )) < 0 )
243249 continue ;
244250 // Parsing environment variable names and values
245251 char * env_name = env ;
@@ -250,14 +256,12 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
250256 * env_value = '\0' ;
251257 env_value ++ ;
252258 // Check if the environment variable name and value are matching
253- if (!strcmp (env_name ,
254- "INIT_SECOND_STAGE" ) &&
259+ if (!strcmp (env_name , "INIT_SECOND_STAGE" ) &&
255260 (!strcmp (env_value , "1" ) ||
256261 !strcmp (env_value , "true" ))) {
257262 pr_info ("/init second_stage executed\n" );
258263 apply_kernelsu_rules ();
259- init_second_stage_executed =
260- true;
264+ init_second_stage_executed = true;
261265 ksu_android_ns_fs_check ();
262266 }
263267 }
@@ -266,11 +270,18 @@ int ksu_handle_execveat_ksud(int *fd, struct filename **filename_ptr,
266270 }
267271
268272 if (unlikely (first_app_process && !memcmp (filename -> name , app_process ,
269- sizeof (app_process ) - 1 ))) {
273+ sizeof (app_process ) - 1 ))) {
270274 first_app_process = false;
271275 pr_info ("exec app_process, /data prepared, second_stage: %d\n" ,
272- init_second_stage_executed );
273- on_post_fs_data (); // we keep this for old ksud
276+ init_second_stage_executed );
277+ struct task_struct * init_task ;
278+ rcu_read_lock ();
279+ init_task = rcu_dereference (current -> parent );
280+ if (init_task ) {
281+ task_work_add (init_task , & on_post_fs_data_cb , TWA_RESUME );
282+ }
283+ rcu_read_unlock ();
284+
274285 stop_execve_hook ();
275286 }
276287
@@ -283,13 +294,12 @@ static struct file_operations fops_proxy;
283294static ssize_t read_count_append = 0 ;
284295
285296static ssize_t read_proxy (struct file * file , char __user * buf , size_t count ,
286- loff_t * pos )
297+ loff_t * pos )
287298{
288299 bool first_read = file -> f_pos == 0 ;
289300 ssize_t ret = orig_read (file , buf , count , pos );
290301 if (first_read ) {
291- pr_info ("read_proxy append %ld + %ld\n" , ret ,
292- read_count_append );
302+ pr_info ("read_proxy append %ld + %ld\n" , ret , read_count_append );
293303 ret += read_count_append ;
294304 }
295305 return ret ;
@@ -300,15 +310,14 @@ static ssize_t read_iter_proxy(struct kiocb *iocb, struct iov_iter *to)
300310 bool first_read = iocb -> ki_pos == 0 ;
301311 ssize_t ret = orig_read_iter (iocb , to );
302312 if (first_read ) {
303- pr_info ("read_iter_proxy append %ld + %ld\n" , ret ,
304- read_count_append );
313+ pr_info ("read_iter_proxy append %ld + %ld\n" , ret , read_count_append );
305314 ret += read_count_append ;
306315 }
307316 return ret ;
308317}
309318
310319static int ksu_handle_vfs_read (struct file * * file_ptr , char __user * * buf_ptr ,
311- size_t * count_ptr , loff_t * * pos )
320+ size_t * count_ptr , loff_t * * pos )
312321{
313322#ifndef CONFIG_KPROBES
314323 if (!ksu_vfs_read_hook ) {
@@ -366,7 +375,7 @@ static int ksu_handle_vfs_read(struct file **file_ptr, char __user **buf_ptr,
366375 size_t rc_count = strlen (KERNEL_SU_RC );
367376
368377 pr_info ("vfs_read: %s, comm: %s, count: %zu, rc_count: %zu\n" , dpath ,
369- current -> comm , count , rc_count );
378+ current -> comm , count , rc_count );
370379
371380 if (count < rc_count ) {
372381 pr_err ("count: %zu < rc_count: %zu\n" , count , rc_count );
@@ -402,7 +411,7 @@ static int ksu_handle_vfs_read(struct file **file_ptr, char __user **buf_ptr,
402411}
403412
404413static int ksu_handle_sys_read (unsigned int fd , char __user * * buf_ptr ,
405- size_t * count_ptr )
414+ size_t * count_ptr )
406415{
407416 struct file * file = fget (fd );
408417 if (!file ) {
@@ -421,7 +430,7 @@ static bool is_volumedown_enough(unsigned int count)
421430}
422431
423432int ksu_handle_input_handle_event (unsigned int * type , unsigned int * code ,
424- int * value )
433+ int * value )
425434{
426435#ifndef CONFIG_KPROBES
427436 if (!ksu_input_hook ) {
@@ -486,8 +495,7 @@ static int sys_execve_handler_pre(struct kprobe *p, struct pt_regs *regs)
486495 filename_in .name = path ;
487496
488497 filename_p = & filename_in ;
489- return ksu_handle_execveat_ksud (AT_FDCWD , & filename_p , & argv , NULL ,
490- NULL );
498+ return ksu_handle_execveat_ksud (AT_FDCWD , & filename_p , & argv , NULL , NULL );
491499}
492500
493501static int sys_read_handler_pre (struct kprobe * p , struct pt_regs * regs )
@@ -501,7 +509,7 @@ static int sys_read_handler_pre(struct kprobe *p, struct pt_regs *regs)
501509}
502510
503511static int input_handle_event_handler_pre (struct kprobe * p ,
504- struct pt_regs * regs )
512+ struct pt_regs * regs )
505513{
506514 unsigned int * type = (unsigned int * )& PT_REGS_PARM2 (regs );
507515 unsigned int * code = (unsigned int * )& PT_REGS_PARM3 (regs );
@@ -519,7 +527,6 @@ static struct kprobe vfs_read_kp = {
519527 .pre_handler = sys_read_handler_pre ,
520528};
521529
522-
523530static struct kprobe input_event_kp = {
524531 .symbol_name = "input_event" ,
525532 .pre_handler = input_handle_event_handler_pre ,
0 commit comments