1 /*
2  * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 
13 #include "tee_auth_common.h"
14 #include <securec.h>
15 #include <stdbool.h>
16 #include "tee_log.h"
17 
18 #ifdef LOG_TAG
19 #undef LOG_TAG
20 #endif
21 #define LOG_TAG "teecd_auth"
22 
ReadCmdLine(const char * path,char * buffer,size_t bufferLen,char * caName,size_t nameLen)23 static int ReadCmdLine(const char *path, char *buffer, size_t bufferLen, char *caName, size_t nameLen)
24 {
25     FILE *fd = fopen(path, "rb");
26     if (fd == NULL) {
27         tloge("fopen is error: %d\n", errno);
28         return -1;
29     }
30     int bytesRead = (int)fread(buffer, sizeof(char), bufferLen - 1, fd);
31     bool readError = (bytesRead <= 0 || (ferror(fd) != 0));
32     if (readError) {
33         tloge("cannot read from cmdline\n");
34         fclose(fd);
35         return -1;
36     }
37     (void)fclose(fd);
38 
39     size_t firstStringLen = strnlen(buffer, bufferLen - 1);
40     errno_t res = strncpy_s(caName, nameLen - 1, buffer, firstStringLen);
41     if (res != EOK) {
42         tloge("copy caName failed\n");
43         return -1;
44     }
45 
46     return bytesRead;
47 }
48 
49 /*
50  * this file "/proc/pid/cmdline" can be modified by any user,
51  * so the package name we get from it is not to be trusted,
52  * the CA authentication strategy does not rely much on the pkgname,
53  * this is mainly to make it compatible with POHNE_PLATFORM
54  */
TeeGetCaName(int caPid,char * caName,size_t nameLen)55 static int TeeGetCaName(int caPid, char *caName, size_t nameLen)
56 {
57     char path[MAX_PATH_LENGTH] = { 0 };
58     char temp[CMD_MAX_SIZE] = { 0 };
59 
60     if (caName == NULL || nameLen == 0) {
61         tloge("input :caName invalid\n");
62         return -1;
63     }
64 
65     int ret = snprintf_s(path, sizeof(path), sizeof(path) - 1, "/proc/%d/cmdline", caPid);
66     if (ret == -1) {
67         tloge("tee get ca name snprintf_s err\n");
68         return ret;
69     }
70 
71     int bytesRead = ReadCmdLine(path, temp, CMD_MAX_SIZE, caName, nameLen);
72 
73     return bytesRead;
74 }
75 
TeeGetPkgName(int caPid,char * path,size_t pathLen)76 int TeeGetPkgName(int caPid, char *path, size_t pathLen)
77 {
78     if (path == NULL || pathLen > MAX_PATH_LENGTH) {
79         tloge("path is null or path len overflow\n");
80         return -1;
81     }
82 
83     if (TeeGetCaName(caPid, path, pathLen) < 0) {
84         tloge("get ca name failed\n");
85         return -1;
86     }
87 
88     return 0;
89 }
90