Skip to content

Commit 6d076ca

Browse files
xiaojingrichwenlingz
authored andcommitted
tools: acrn-crashlog: remove unsafe apis in usercrash
Since strlen/vsnprintf/ato* api are not safe, so use strnlen instead of strlen, use vasprintf instead of vsnprintf and use strtol instead of atoi. Tracked-On: #1254 Signed-off-by: xiaojin2 <xiaojing.liu@intel.com> Reviewed-by: Huang Yonghua <yonghua.huang@intel.com> Reviewed-by: Liu, Xinwu <xinwu.liu@intel.com> Acked-by: Chen Gang <gang.c.chen@intel.com>
1 parent 8f7fa50 commit 6d076ca

File tree

6 files changed

+45
-44
lines changed

6 files changed

+45
-44
lines changed

tools/acrn-crashlog/usercrash/client.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ static int usercrashd_connect(int pid, int *usercrashd_socket,
8585
LOGE("crash process name is NULL\n");
8686
return -1;
8787
}
88-
sockfd = socket_local_client(SOCKET_NAME, SOCK_SEQPACKET);
88+
sockfd = socket_local_client(SOCKET_NAME, strlen(SOCKET_NAME),
89+
SOCK_SEQPACKET);
8990
if (sockfd == -1) {
9091
LOGE("failed to connect to usercrashd, error (%s)\n",
9192
strerror(errno));
@@ -212,8 +213,8 @@ int main(int argc, char *argv[])
212213

213214
if (argc == 4) {
214215
/* it's from coredump */
215-
pid = atoi(argv[1]);
216-
sig = atoi(argv[3]);
216+
pid = (int)strtol(argv[1], NULL, 10);
217+
sig = (int)strtol(argv[3], NULL, 10);
217218
ret = usercrashd_connect(pid, &sock, &out_fd, argv[2]);
218219
if (ret) {
219220
LOGE("usercrashd_connect failed, error (%s)\n",

tools/acrn-crashlog/usercrash/crash_dump.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,29 @@
2424
#define DUMP_FILE "/tmp/core"
2525
#define BUFFER_SIZE 8196
2626
#define LINK_LEN 512
27+
#define TID "Tgid:"
2728
/* 128 means the length of the DUMP_FILE */
2829
#define FORMAT_LENGTH (LINK_LEN + 128)
2930

3031
static void loginfo(int fd, const char *fmt, ...)
3132
{
32-
char buf[512];
33+
char *buf;
3334
va_list ap;
34-
size_t len, ret;
35+
int len, ret;
3536

3637
va_start(ap, fmt);
37-
vsnprintf(buf, sizeof(buf), fmt, ap);
38-
va_end(ap);
39-
40-
len = strlen(buf);
41-
if (len <= 0)
38+
len = vasprintf(&buf, fmt, ap);
39+
if (len == -1) {
40+
LOGE("write to buf failed\n");
4241
return;
42+
}
43+
va_end(ap);
4344

4445
ret = write(fd, buf, len);
4546
if (ret != len) {
4647
LOGE("write in loginfo failed\n");
4748
}
49+
free(buf);
4850
}
4951

5052
static const char *get_signame(int sig)
@@ -249,7 +251,7 @@ static int save_usercrash_file(int pid, int tgid, const char *comm,
249251
}
250252

251253
static int get_key_value(int pid, const char *path, const char *key,
252-
char *value, size_t value_len)
254+
const size_t klen, char *value, const size_t value_len)
253255
{
254256
int len;
255257
int ret;
@@ -276,7 +278,7 @@ static int get_key_value(int pid, const char *path, const char *key,
276278
end = strchr(msg, '\n');
277279
if (end == NULL)
278280
end = data + size;
279-
start = msg + strlen(key);
281+
start = msg + klen;
280282
len = end - start;
281283
if (len >= (int)value_len) {
282284
free(data);
@@ -312,12 +314,13 @@ void crash_dump(int pid, int sig, int out_fd)
312314
}
313315
comm[ret] = '\0';
314316

315-
ret = get_key_value(pid, GET_TID, "Tgid:", result, sizeof(result));
317+
ret = get_key_value(pid, GET_TID, TID, strlen(TID),
318+
result, sizeof(result));
316319
if (ret) {
317320
LOGE("get Tgid error\n");
318321
return;
319322
}
320-
tgid = atoi(result);
323+
tgid = (int)strtol(result, NULL, 10);
321324
if (!sig)
322325
sig = DEBUGGER_SIGNAL;
323326
if (save_usercrash_file(pid, tgid, comm, sig, out_fd))

tools/acrn-crashlog/usercrash/debugger.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ int main(int argc, char *argv[])
5353

5454
if (argc == 2) {
5555
/* it's from shell cmd */
56-
pid = atoi(argv[1]);
56+
pid = (int)strtol(argv[1], NULL, 10);
5757
crash_dump(pid, 0, STDOUT_FILENO);
5858
} else {
5959
print_usage();

tools/acrn-crashlog/usercrash/include/protocol.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
#include <sys/cdefs.h>
1515

1616
#define RESERVED_SOCKET_PREFIX "/tmp/"
17+
#define SOCKET_PATH_MAX 128
1718

18-
int linux_get_control_socket(const char *name);
19+
int create_socket_server(const char *name, int type);
1920

20-
int socket_local_client(const char *name, int type);
21+
int socket_local_client(const char *name, const size_t len, int type);
2122
ssize_t send_fd(int sockfd, const void *data, size_t len, int fd);
2223
ssize_t recv_fd(int sockfd, void *data, size_t len, int *out_fd);
2324

tools/acrn-crashlog/usercrash/protocol.c

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,21 @@
3131
(sizeof(struct sockaddr_un) - offsetof(struct sockaddr_un, sun_path))
3232

3333
/* Documented in header file. */
34-
static int socket_make_sockaddr_un(const char *name,
34+
static int socket_make_sockaddr_un(const char *name, const size_t len,
3535
struct sockaddr_un *p_addr, socklen_t *alen)
3636
{
37-
size_t name_len;
3837
size_t socket_len;
3938

4039
socket_len = strlen(RESERVED_SOCKET_PREFIX);
4140
if (socket_len >= SUN_PATH_MAX)
4241
return -1;
4342
strncpy(p_addr->sun_path, RESERVED_SOCKET_PREFIX, socket_len + 1);
44-
name_len = strlen(name);
45-
if (name_len >= (SUN_PATH_MAX - socket_len))
43+
if (len >= (SUN_PATH_MAX - socket_len))
4644
return -1;
47-
strncat(p_addr->sun_path, name, name_len);
45+
strncat(p_addr->sun_path, name, len);
4846

4947
p_addr->sun_family = AF_LOCAL;
50-
*alen = name_len + socket_len +
48+
*alen = len + socket_len +
5149
offsetof(struct sockaddr_un, sun_path) + 1;
5250
return 0;
5351
}
@@ -58,13 +56,14 @@ static int socket_make_sockaddr_un(const char *name,
5856
* fd is not closed on error. that's your job.
5957
*
6058
*/
61-
static int socket_local_client_connect(int fd, const char *name)
59+
static int socket_local_client_connect(int fd, const char *name,
60+
const size_t len)
6261
{
6362
struct sockaddr_un addr;
6463
socklen_t alen;
6564
int err;
6665

67-
err = socket_make_sockaddr_un(name, &addr, &alen);
66+
err = socket_make_sockaddr_un(name, len, &addr, &alen);
6867

6968
if (err < 0)
7069
goto error;
@@ -85,15 +84,15 @@ static int socket_local_client_connect(int fd, const char *name)
8584
* connect to peer named "name"
8685
* returns fd or -1 on error
8786
*/
88-
int socket_local_client(const char *name, int type)
87+
int socket_local_client(const char *name, const size_t len, int type)
8988
{
9089
int s;
9190

9291
s = socket(AF_LOCAL, type, 0);
9392
if (s < 0)
9493
return -1;
9594

96-
if (socket_local_client_connect(s, name) < 0) {
95+
if (socket_local_client_connect(s, name, len) < 0) {
9796
close(s);
9897
return -1;
9998
}
@@ -108,20 +107,20 @@ static int socket_bind(int fd, const char *name)
108107
size_t name_len;
109108

110109
addr.sun_family = AF_UNIX;
111-
name_len = strlen(name);
110+
name_len = strnlen(name, SOCKET_PATH_MAX);
112111
if (name_len >= SUN_PATH_MAX)
113112
return -1;
114113
strncpy(addr.sun_path, name, name_len + 1);
115114
unlink(addr.sun_path);
116-
alen = strlen(addr.sun_path) + sizeof(addr.sun_family);
115+
alen = strnlen(addr.sun_path, SUN_PATH_MAX) + sizeof(addr.sun_family);
117116

118117
if (bind(fd, (struct sockaddr *)&addr, alen) == -1)
119118
return -1;
120119

121120
return fd;
122121
}
123122

124-
static int create_socket_server(const char *name, int type)
123+
int create_socket_server(const char *name, int type)
125124
{
126125
int err;
127126
int fd;
@@ -140,15 +139,6 @@ static int create_socket_server(const char *name, int type)
140139
return fd;
141140
}
142141

143-
int linux_get_control_socket(const char *name)
144-
{
145-
char socketName[64];
146-
147-
snprintf(socketName, sizeof(socketName), RESERVED_SOCKET_PREFIX "%s",
148-
name);
149-
return create_socket_server(socketName, SOCK_SEQPACKET);
150-
}
151-
152142
ssize_t send_fd(int sockfd, const void *data, size_t len, int fd)
153143
{
154144
char cmsg_buf[CMSG_SPACE(sizeof(int))];

tools/acrn-crashlog/usercrash/server.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "protocol.h"
4444
#include "log_sys.h"
4545
#include "version.h"
46+
#include "strutils.h"
4647

4748
#define FILE_PATH_LEN_MAX 256
4849

@@ -377,9 +378,10 @@ static void print_usage(void)
377378

378379
int main(int argc, char *argv[])
379380
{
380-
char socket_path[128];
381+
char socket_path[SOCKET_PATH_MAX];
381382
DIR *dir;
382383
int fd;
384+
int len;
383385
evutil_socket_t crash_socket;
384386
int opt;
385387
struct sigaction action;
@@ -434,22 +436,26 @@ int main(int argc, char *argv[])
434436
debuggerd_register_handlers(&action);
435437

436438
find_oldest_usercrash();
437-
439+
len = snprintf(socket_path, sizeof(socket_path), "%s/%s",
440+
RESERVED_SOCKET_PREFIX, SOCKET_NAME);
441+
if (s_not_expect(len , sizeof(socket_path))) {
442+
LOGE("construct socket path error\n");
443+
exit(EXIT_FAILURE);
444+
}
438445
/**
439446
* evutil_socket_t on other platform except WIN32 platform is int
440447
* type, but on WIN32 platform evutil_socket_t is intptr_t type.
441448
* So, considering compatibility, here need to transfer socket fd to
442449
* evutil_socket_t type.
443450
*/
444-
crash_socket = (evutil_socket_t)linux_get_control_socket(SOCKET_NAME);
451+
crash_socket = (evutil_socket_t)create_socket_server(socket_path,
452+
SOCK_SEQPACKET);
445453

446454
if (crash_socket == -1) {
447455
LOGE("failed to get socket from init, error (%s)\n",
448456
strerror(errno));
449457
exit(EXIT_FAILURE);
450458
}
451-
snprintf(socket_path, sizeof(socket_path), "%s/%s",
452-
RESERVED_SOCKET_PREFIX, SOCKET_NAME);
453459
if (chmod(socket_path, 0622) == -1) {
454460
LOGE("failed to change usercrashd_crash priority\n");
455461
goto fail;

0 commit comments

Comments
 (0)