• 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 #include "syscall_pub.h"
32 
CheckRegion(const LosVmSpace * space,VADDR_T ptr,size_t len)33 int CheckRegion(const LosVmSpace *space, VADDR_T ptr, size_t len)
34 {
35     LosVmMapRegion *region = LOS_RegionFind((LosVmSpace *)space, ptr);
36     if (region == NULL) {
37         return -1;
38     }
39     if (ptr + len <= region->range.base + region->range.size) {
40         return 0;
41     }
42     return CheckRegion(space, region->range.base + region->range.size,
43                        (ptr + len) - (region->range.base + region->range.size));
44 }
45 
DupUserMem(const void * ptr,size_t len,int needCopy)46 void *DupUserMem(const void *ptr, size_t len, int needCopy)
47 {
48     void *p = LOS_MemAlloc(OS_SYS_MEM_ADDR, len);
49 
50     if (p == NULL) {
51         set_errno(ENOMEM);
52         return NULL;
53     }
54 
55     if (needCopy && LOS_ArchCopyFromUser(p, ptr, len) != 0) {
56         LOS_MemFree(OS_SYS_MEM_ADDR, p);
57         set_errno(EFAULT);
58         return NULL;
59     }
60 
61     return p;
62 }
63 
GetFullpath(int fd,const char * path,char ** fullpath)64 int GetFullpath(int fd, const char *path, char **fullpath)
65 {
66     int ret = 0;
67     char *pathRet = NULL;
68     struct file *file = NULL;
69     struct stat bufRet = {0};
70 
71     if (path != NULL) {
72         ret = UserPathCopy(path, &pathRet);
73         if (ret != 0) {
74             goto OUT;
75         }
76     }
77 
78     if ((pathRet != NULL) && (*pathRet == '/')) {
79         *fullpath = pathRet;
80         pathRet = NULL;
81     } else {
82         if (fd != AT_FDCWD) {
83             /* Process fd convert to system global fd */
84             fd = GetAssociatedSystemFd(fd);
85         }
86         ret = fs_getfilep(fd, &file);
87         if (file) {
88             ret = stat(file->f_path, &bufRet);
89             if (!ret) {
90                 if (!S_ISDIR(bufRet.st_mode)) {
91                     set_errno(ENOTDIR);
92                     ret = -ENOTDIR;
93                     goto OUT;
94                 }
95             }
96         }
97         ret = vfs_normalize_pathat(fd, pathRet, fullpath);
98     }
99 
100 OUT:
101     PointerFree(pathRet);
102     return ret;
103 }
104 
UserPathCopy(const char * userPath,char ** pathBuf)105 int UserPathCopy(const char *userPath, char **pathBuf)
106 {
107     int ret;
108 
109     *pathBuf = (char *)LOS_MemAlloc(OS_SYS_MEM_ADDR, PATH_MAX + 1);
110     if (*pathBuf == NULL) {
111         return -ENOMEM;
112     }
113 
114     ret = LOS_StrncpyFromUser(*pathBuf, userPath, PATH_MAX + 1);
115     if (ret < 0) {
116         (void)LOS_MemFree(OS_SYS_MEM_ADDR, *pathBuf);
117         *pathBuf = NULL;
118         return ret;
119     } else if (ret > PATH_MAX) {
120         (void)LOS_MemFree(OS_SYS_MEM_ADDR, *pathBuf);
121         *pathBuf = NULL;
122         return -ENAMETOOLONG;
123     }
124     (*pathBuf)[ret] = '\0';
125 
126     return 0;
127 }
128