mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 00:42:44 +00:00 
			
		
		
		
	Kernel: Use Userspace<T> for the futex syscall
Utilizie Userspace<T> for the syscall argument itself, as well as internally in the SC_futex_params struct. We were double validating the SC_futex_params.timeout validation, that was removed as well.
This commit is contained in:
		
							parent
							
								
									a7a7e6245f
								
							
						
					
					
						commit
						fa666f6897
					
				
					 3 changed files with 18 additions and 26 deletions
				
			
		|  | @ -331,10 +331,10 @@ struct SC_getpeername_params { | |||
| }; | ||||
| 
 | ||||
| struct SC_futex_params { | ||||
|     i32* userspace_address; | ||||
|     Userspace<const i32*> userspace_address; | ||||
|     int futex_op; | ||||
|     i32 val; | ||||
|     const timespec* timeout; | ||||
|     Userspace<const timespec*> timeout; | ||||
| }; | ||||
| 
 | ||||
| struct SC_setkeymap_params { | ||||
|  |  | |||
|  | @ -321,7 +321,7 @@ public: | |||
|     int sys$module_unload(const char* name, size_t name_length); | ||||
|     int sys$profiling_enable(pid_t); | ||||
|     int sys$profiling_disable(pid_t); | ||||
|     int sys$futex(const Syscall::SC_futex_params*); | ||||
|     int sys$futex(Userspace<const Syscall::SC_futex_params*>); | ||||
|     int sys$set_thread_boost(int tid, int amount); | ||||
|     int sys$set_process_boost(pid_t, int amount); | ||||
|     int sys$chroot(const char* path, size_t path_length, int mount_flags); | ||||
|  | @ -688,7 +688,7 @@ private: | |||
|     VeilState m_veil_state { VeilState::None }; | ||||
|     Vector<UnveiledPath> m_unveiled_paths; | ||||
| 
 | ||||
|     WaitQueue& futex_queue(i32*); | ||||
|     WaitQueue& futex_queue(Userspace<const i32*>); | ||||
|     HashMap<u32, OwnPtr<WaitQueue>> m_futex_queues; | ||||
| 
 | ||||
|     OwnPtr<PerformanceEventBuffer> m_perf_event_buffer; | ||||
|  |  | |||
|  | @ -42,15 +42,15 @@ void compute_relative_timeout_from_absolute(const timespec& absolute_time, timev | |||
|     compute_relative_timeout_from_absolute(tv_absolute_time, relative_time); | ||||
| } | ||||
| 
 | ||||
| WaitQueue& Process::futex_queue(i32* userspace_address) | ||||
| WaitQueue& Process::futex_queue(Userspace<const i32*> userspace_address) | ||||
| { | ||||
|     auto& queue = m_futex_queues.ensure((FlatPtr)userspace_address); | ||||
|     auto& queue = m_futex_queues.ensure(userspace_address.ptr()); | ||||
|     if (!queue) | ||||
|         queue = make<WaitQueue>(); | ||||
|     return *queue; | ||||
| } | ||||
| 
 | ||||
| int Process::sys$futex(const Syscall::SC_futex_params* user_params) | ||||
| int Process::sys$futex(Userspace<const Syscall::SC_futex_params*> user_params) | ||||
| { | ||||
|     REQUIRE_PROMISE(thread); | ||||
| 
 | ||||
|  | @ -58,37 +58,29 @@ int Process::sys$futex(const Syscall::SC_futex_params* user_params) | |||
|     if (!validate_read_and_copy_typed(¶ms, user_params)) | ||||
|         return -EFAULT; | ||||
| 
 | ||||
|     i32* userspace_address = params.userspace_address; | ||||
|     int futex_op = params.futex_op; | ||||
|     i32 value = params.val; | ||||
|     const timespec* user_timeout = params.timeout; | ||||
| 
 | ||||
|     if (!validate_read_typed(userspace_address)) | ||||
|     if (!validate_read_typed(params.userspace_address)) | ||||
|         return -EFAULT; | ||||
| 
 | ||||
|     if (user_timeout && !validate_read_typed(user_timeout)) | ||||
|         return -EFAULT; | ||||
| 
 | ||||
|     switch (futex_op) { | ||||
|     switch (params.futex_op) { | ||||
|     case FUTEX_WAIT: { | ||||
|         i32 user_value; | ||||
|         copy_from_user(&user_value, userspace_address); | ||||
|         if (user_value != value) | ||||
|         copy_from_user(&user_value, params.userspace_address); | ||||
|         if (user_value != params.val) | ||||
|             return -EAGAIN; | ||||
| 
 | ||||
|         timespec ts_abstimeout { 0, 0 }; | ||||
|         if (user_timeout && !validate_read_and_copy_typed(&ts_abstimeout, user_timeout)) | ||||
|         if (params.timeout && !validate_read_and_copy_typed(&ts_abstimeout, params.timeout)) | ||||
|             return -EFAULT; | ||||
| 
 | ||||
|         WaitQueue& wait_queue = futex_queue(userspace_address); | ||||
|         timeval* optional_timeout = nullptr; | ||||
|         timeval relative_timeout { 0, 0 }; | ||||
|         if (user_timeout) { | ||||
|         if (params.timeout) { | ||||
|             compute_relative_timeout_from_absolute(ts_abstimeout, relative_timeout); | ||||
|             optional_timeout = &relative_timeout; | ||||
|         } | ||||
| 
 | ||||
|         // FIXME: This is supposed to be interruptible by a signal, but right now WaitQueue cannot be interrupted.
 | ||||
|         WaitQueue& wait_queue = futex_queue(params.userspace_address); | ||||
|         Thread::BlockResult result = Thread::current()->wait_on(wait_queue, "Futex", optional_timeout); | ||||
|         if (result == Thread::BlockResult::InterruptedByTimeout) { | ||||
|             return -ETIMEDOUT; | ||||
|  | @ -97,12 +89,12 @@ int Process::sys$futex(const Syscall::SC_futex_params* user_params) | |||
|         break; | ||||
|     } | ||||
|     case FUTEX_WAKE: | ||||
|         if (value == 0) | ||||
|         if (params.val == 0) | ||||
|             return 0; | ||||
|         if (value == 1) { | ||||
|             futex_queue(userspace_address).wake_one(); | ||||
|         if (params.val == 1) { | ||||
|             futex_queue(params.userspace_address).wake_one(); | ||||
|         } else { | ||||
|             futex_queue(userspace_address).wake_n(value); | ||||
|             futex_queue(params.userspace_address).wake_n(params.val); | ||||
|         } | ||||
|         break; | ||||
|     } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Brian Gianforcaro
						Brian Gianforcaro