Skip to content

Commit cbcc769

Browse files
author
git-core
committed
Don't use mktemp
After access to the su cache was secured (commit 7d97cfe), we can use simple name convention for the socket object. Name .socket<pid> is simple enough and doesn't require mktemp which is blamed by gcc on every linkage.
1 parent 73a4964 commit cbcc769

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

su.c

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@
4141

4242
#include "su.h"
4343

44-
//extern char* _mktemp(char*); /* mktemp doesn't link right. Don't ask me why. */
45-
4644
/* Still lazt, will fix this */
47-
static char *socket_path = NULL;
45+
static char socket_path[PATH_MAX];
4846

4947
static struct su_initiator su_from = {
5048
.pid = -1,
@@ -139,11 +137,9 @@ static void cleanup_signal(int sig)
139137
exit(sig);
140138
}
141139

142-
static int socket_create_temp(void)
140+
static int socket_create_temp(char *path, size_t len)
143141
{
144-
static char buf[PATH_MAX];
145142
int fd;
146-
147143
struct sockaddr_un sun;
148144

149145
fd = socket(AF_LOCAL, SOCK_STREAM, 0);
@@ -152,29 +148,32 @@ static int socket_create_temp(void)
152148
return -1;
153149
}
154150

155-
for (;;) {
156-
memset(&sun, 0, sizeof(sun));
157-
sun.sun_family = AF_LOCAL;
158-
strcpy(buf, SOCKET_PATH_TEMPLATE);
159-
socket_path = mktemp(buf);
160-
snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", socket_path);
161-
162-
if (bind(fd, (struct sockaddr*)&sun, sizeof(sun)) < 0) {
163-
if (errno != EADDRINUSE) {
164-
PLOGE("bind");
165-
return -1;
166-
}
167-
} else {
168-
break;
169-
}
151+
memset(&sun, 0, sizeof(sun));
152+
sun.sun_family = AF_LOCAL;
153+
snprintf(path, len, "%s/.socket%d", REQUESTOR_CACHE_PATH, getpid());
154+
snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", path);
155+
156+
/*
157+
* Delete the socket to protect from situations when
158+
* something bad occured previously and the kernel reused pid from that process.
159+
* Small probability, isn't it.
160+
*/
161+
unlink(sun.sun_path);
162+
163+
if (bind(fd, (struct sockaddr*)&sun, sizeof(sun)) < 0) {
164+
PLOGE("bind");
165+
goto err;
170166
}
171167

172168
if (listen(fd, 1) < 0) {
173169
PLOGE("listen");
174-
return -1;
170+
goto err;
175171
}
176172

177173
return fd;
174+
err:
175+
close(fd);
176+
return -1;
178177
}
179178

180179
static int socket_accept(int serv_fd)
@@ -397,7 +396,7 @@ int main(int argc, char *argv[])
397396
default: deny();
398397
}
399398

400-
socket_serv_fd = socket_create_temp();
399+
socket_serv_fd = socket_create_temp(socket_path, sizeof(socket_path));
401400
if (socket_serv_fd < 0) {
402401
deny();
403402
}

su.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727

2828
#define DEFAULT_COMMAND "/system/bin/sh"
2929

30-
#define SOCKET_PATH_TEMPLATE REQUESTOR_CACHE_PATH "/.socketXXXXXX"
31-
3230
#define VERSION "3.0.3.2"
3331
#define VERSION_CODE 15
3432

0 commit comments

Comments
 (0)