1 // Copyright 2016 The Fuchsia Authors
2 //
3 // Use of this source code is governed by a MIT-style
4 // license that can be found in the LICENSE file or at
5 // https://opensource.org/licenses/MIT
6 
7 #include <err.h>
8 #include <inttypes.h>
9 #include <trace.h>
10 
11 #include <object/handle.h>
12 #include <object/process_dispatcher.h>
13 
14 #include "priv.h"
15 
16 #define LOCAL_TRACE 0
17 
18 // zx_status_t zx_handle_close
sys_handle_close(zx_handle_t handle_value)19 zx_status_t sys_handle_close(zx_handle_t handle_value) {
20     LTRACEF("handle %x\n", handle_value);
21 
22     // Closing the "never a handle" invalid handle is not an error
23     // It's like free(NULL).
24     if (handle_value == ZX_HANDLE_INVALID)
25         return ZX_OK;
26     auto up = ProcessDispatcher::GetCurrent();
27     HandleOwner handle(up->RemoveHandle(handle_value));
28     if (!handle)
29         return ZX_ERR_BAD_HANDLE;
30     return ZX_OK;
31 }
32 
33 // zx_status_t zx_handle_close_many
sys_handle_close_many(user_in_ptr<const zx_handle_t> handles,size_t num_handles)34 zx_status_t sys_handle_close_many(user_in_ptr<const zx_handle_t> handles, size_t num_handles) {
35     LTRACEF("handles %p, num_handles %zu\n", handles.get(), num_handles);
36 
37     auto up = ProcessDispatcher::GetCurrent();
38     return up->RemoveHandles(handles, num_handles);
39 }
40 
handle_dup_replace(bool is_replace,zx_handle_t handle_value,zx_rights_t rights,user_out_handle * out)41 static zx_status_t handle_dup_replace(
42     bool is_replace, zx_handle_t handle_value, zx_rights_t rights,
43     user_out_handle* out) {
44     LTRACEF("handle %x\n", handle_value);
45 
46     auto up = ProcessDispatcher::GetCurrent();
47 
48     Guard<fbl::Mutex> guard{up->handle_table_lock()};
49     auto source = up->GetHandleLocked(handle_value);
50     if (!source)
51         return ZX_ERR_BAD_HANDLE;
52 
53     if (!is_replace) {
54         if (!source->HasRights(ZX_RIGHT_DUPLICATE))
55             return ZX_ERR_ACCESS_DENIED;
56     }
57 
58     if (rights == ZX_RIGHT_SAME_RIGHTS) {
59         rights = source->rights();
60     } else if ((source->rights() & rights) != rights) {
61         if (is_replace)
62             up->RemoveHandleLocked(handle_value);
63         return ZX_ERR_INVALID_ARGS;
64     }
65 
66     zx_status_t status = out->dup(source, rights);
67 
68     if (is_replace)
69         up->RemoveHandleLocked(handle_value);
70 
71     return status;
72 }
73 
74 // zx_status_t zx_handle_duplicate
sys_handle_duplicate(zx_handle_t handle_value,zx_rights_t rights,user_out_handle * out)75 zx_status_t sys_handle_duplicate(
76     zx_handle_t handle_value, zx_rights_t rights, user_out_handle* out) {
77     return handle_dup_replace(false, handle_value, rights, out);
78 }
79 
80 // zx_status_t zx_handle_replace
sys_handle_replace(zx_handle_t handle_value,zx_rights_t rights,user_out_handle * out)81 zx_status_t sys_handle_replace(
82     zx_handle_t handle_value, zx_rights_t rights, user_out_handle* out) {
83     return handle_dup_replace(true, handle_value, rights, out);
84 }
85