• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "capability_type.h"
33 #include "los_memory.h"
34 #include "los_process_pri.h"
35 #include "user_copy.h"
36 #include "los_printf.h"
37 
38 #define CAPABILITY_INIT_STAT            0xffffffff
39 #define CAPABILITY_GET_CAP_MASK(x)      (1 << ((x) & 31))
40 #define CAPABILITY_MAX                  31
41 #define VALID_CAPS(a, b)                (((a) & (~(b))) != 0)
42 
IsCapPermit(UINT32 capIndex)43 BOOL IsCapPermit(UINT32 capIndex)
44 {
45     UINT32 capability = OsCurrProcessGet()->capability;
46     if (capIndex > CAPABILITY_MAX || capIndex < 0) {
47         PRINTK("%s,%d, get invalid capIndex %u\n", __FUNCTION__, __LINE__, capIndex);
48         return FALSE;
49     }
50 
51     return (capability & (CAPABILITY_GET_CAP_MASK(capIndex)));
52 }
53 
OsInitCapability(LosProcessCB * processCB)54 VOID OsInitCapability(LosProcessCB *processCB)
55 {
56     processCB->capability = CAPABILITY_INIT_STAT;
57 }
58 
OsCopyCapability(LosProcessCB * from,LosProcessCB * to)59 VOID OsCopyCapability(LosProcessCB *from, LosProcessCB *to)
60 {
61     UINT32 intSave;
62 
63     SCHEDULER_LOCK(intSave);
64     to->capability = from->capability;
65     SCHEDULER_UNLOCK(intSave);
66 }
67 
SysCapSet(UINT32 caps)68 UINT32 SysCapSet(UINT32 caps)
69 {
70     UINT32 intSave;
71 
72     SCHEDULER_LOCK(intSave);
73     if (!IsCapPermit(CAP_CAPSET)) {
74         SCHEDULER_UNLOCK(intSave);
75         return -EPERM;
76     }
77 
78     if (VALID_CAPS(caps, OsCurrProcessGet()->capability)) {
79         SCHEDULER_UNLOCK(intSave);
80         return -EPERM;
81     }
82 
83     OsCurrProcessGet()->capability = caps;
84     SCHEDULER_UNLOCK(intSave);
85     return LOS_OK;
86 }
87 
SysCapGet(pid_t pid,UINT32 * caps)88 UINT32 SysCapGet(pid_t pid, UINT32 *caps)
89 {
90     UINT32 intSave;
91     UINT32 kCaps;
92     LosProcessCB *processCB = NULL;
93 
94     if ((OS_PID_CHECK_INVALID((UINT32)pid))) {
95         return -EINVAL;
96     }
97 
98     if (pid == 0) {
99         processCB = OsCurrProcessGet();
100     } else {
101         processCB = OS_PCB_FROM_PID(pid);
102     }
103 
104     SCHEDULER_LOCK(intSave);
105     if (OsProcessIsInactive(processCB)) {
106         SCHEDULER_UNLOCK(intSave);
107         return -ESRCH;
108     }
109 
110     kCaps = processCB->capability;
111     SCHEDULER_UNLOCK(intSave);
112 
113     if (LOS_ArchCopyToUser(caps, &kCaps, sizeof(UINT32)) != LOS_OK) {
114         return -EFAULT;
115     }
116 
117     return LOS_OK;
118 }
119