OSDN Git Service

DO NOT MERGE Fix security vulnerability in pre-O rild code. am: c4c7c551b1 -s ours...
[android-x86/hardware-ril.git] / rild / rild.c
1 /* //device/system/rild/rild.c
2 **
3 ** Copyright 2006 The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <dlfcn.h>
21 #include <string.h>
22 #include <stdint.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <errno.h>
26
27 #include <telephony/ril.h>
28 #define LOG_TAG "RILD"
29 #include <utils/Log.h>
30 #include <cutils/properties.h>
31 #include <cutils/sockets.h>
32 #include <sys/capability.h>
33 #include <sys/prctl.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <libril/ril_ex.h>
37
38 #include <private/android_filesystem_config.h>
39 #include "hardware/qemu_pipe.h"
40
41 #define LIB_PATH_PROPERTY   "rild.libpath"
42 #define LIB_ARGS_PROPERTY   "rild.libargs"
43 #define MAX_LIB_ARGS        16
44 #define MAX_CAP_NUM         (CAP_TO_INDEX(CAP_LAST_CAP) + 1)
45
46 static void usage(const char *argv0) {
47     fprintf(stderr, "Usage: %s -l <ril impl library> [-- <args for impl library>]\n", argv0);
48     exit(EXIT_FAILURE);
49 }
50
51 extern char rild[MAX_SOCKET_NAME_LENGTH];
52
53 extern void RIL_register (const RIL_RadioFunctions *callbacks);
54
55 extern void RIL_register_socket (RIL_RadioFunctions *(*rilUimInit)
56         (const struct RIL_Env *, int, char **), RIL_SOCKET_TYPE socketType, int argc, char **argv);
57
58 extern void RIL_onRequestComplete(RIL_Token t, RIL_Errno e,
59         void *response, size_t responselen);
60
61 extern void RIL_setRilSocketName(char *);
62
63 #if defined(ANDROID_MULTI_SIM)
64 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
65         size_t datalen, RIL_SOCKET_ID socket_id);
66 #else
67 extern void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
68         size_t datalen);
69 #endif
70
71 extern void RIL_requestTimedCallback (RIL_TimedCallback callback,
72         void *param, const struct timeval *relativeTime);
73
74
75 static struct RIL_Env s_rilEnv = {
76     RIL_onRequestComplete,
77     RIL_onUnsolicitedResponse,
78     RIL_requestTimedCallback
79 };
80
81 extern void RIL_startEventLoop();
82
83 static int make_argv(char * args, char ** argv) {
84     // Note: reserve argv[0]
85     int count = 1;
86     char * tok;
87     char * s = args;
88
89     while ((tok = strtok(s, " \0"))) {
90         argv[count] = tok;
91         s = NULL;
92         count++;
93     }
94     return count;
95 }
96
97 /*
98  * switchUser - Switches UID to radio, preserving CAP_NET_ADMIN capabilities.
99  * Our group, cache, was set by init.
100  */
101 void switchUser() {
102     char debuggable[PROP_VALUE_MAX];
103
104     prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
105     setuid(AID_RADIO);
106
107     struct __user_cap_header_struct header;
108     memset(&header, 0, sizeof(header));
109     header.version = _LINUX_CAPABILITY_VERSION_3;
110     header.pid = 0;
111
112     struct __user_cap_data_struct data[MAX_CAP_NUM];
113     memset(&data, 0, sizeof(data));
114
115     data[CAP_TO_INDEX(CAP_NET_ADMIN)].effective |= CAP_TO_MASK(CAP_NET_ADMIN);
116     data[CAP_TO_INDEX(CAP_NET_ADMIN)].permitted |= CAP_TO_MASK(CAP_NET_ADMIN);
117
118     data[CAP_TO_INDEX(CAP_NET_RAW)].effective |= CAP_TO_MASK(CAP_NET_RAW);
119     data[CAP_TO_INDEX(CAP_NET_RAW)].permitted |= CAP_TO_MASK(CAP_NET_RAW);
120
121     data[CAP_TO_INDEX(CAP_BLOCK_SUSPEND)].effective |= CAP_TO_MASK(CAP_BLOCK_SUSPEND);
122     data[CAP_TO_INDEX(CAP_BLOCK_SUSPEND)].permitted |= CAP_TO_MASK(CAP_BLOCK_SUSPEND);
123
124     if (capset(&header, &data[0]) == -1) {
125         RLOGE("capset failed: %s", strerror(errno));
126         exit(EXIT_FAILURE);
127     }
128
129     /*
130      * Debuggable build only:
131      * Set DUMPABLE that was cleared by setuid() to have tombstone on RIL crash
132      */
133     property_get("ro.debuggable", debuggable, "0");
134     if (strcmp(debuggable, "1") == 0) {
135         prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
136     }
137 }
138
139 int main(int argc, char **argv) {
140     const char * rilLibPath = NULL;
141     char **rilArgv;
142     void *dlHandle;
143     const RIL_RadioFunctions *(*rilInit)(const struct RIL_Env *, int, char **);
144     const RIL_RadioFunctions *(*rilUimInit)(const struct RIL_Env *, int, char **);
145     char *err_str = NULL;
146
147     const RIL_RadioFunctions *funcs;
148     char libPath[PROPERTY_VALUE_MAX];
149     unsigned char hasLibArgs = 0;
150
151     int i;
152     const char *clientId = NULL;
153     RLOGD("**RIL Daemon Started**");
154     RLOGD("**RILd param count=%d**", argc);
155
156     umask(S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH);
157     for (i = 1; i < argc ;) {
158         if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) {
159             rilLibPath = argv[i + 1];
160             i += 2;
161         } else if (0 == strcmp(argv[i], "--")) {
162             i++;
163             hasLibArgs = 1;
164             break;
165         } else if (0 == strcmp(argv[i], "-c") &&  (argc - i > 1)) {
166             clientId = argv[i+1];
167             i += 2;
168         } else {
169             usage(argv[0]);
170         }
171     }
172
173     if (clientId == NULL) {
174         clientId = "0";
175     } else if (atoi(clientId) >= MAX_RILDS) {
176         RLOGE("Max Number of rild's supported is: %d", MAX_RILDS);
177         exit(0);
178     }
179     if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) {
180         RIL_setRilSocketName(strncat(rild, clientId, MAX_SOCKET_NAME_LENGTH));
181     }
182
183     if (rilLibPath == NULL) {
184         if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) {
185             // No lib sepcified on the command line, and nothing set in props.
186             // Assume "no-ril" case.
187             goto done;
188         } else {
189             rilLibPath = libPath;
190         }
191     }
192
193     /* special override when in the emulator */
194 #if 1
195     {
196         static char*  arg_overrides[5];
197         static char   arg_device[32];
198         int           done = 0;
199
200 #define  REFERENCE_RIL_PATH  "libreference-ril.so"
201
202         /* first, read /proc/cmdline into memory */
203         char          buffer[1024], *p, *q;
204         int           len;
205         int           fd = open("/proc/cmdline",O_RDONLY);
206
207         if (fd < 0) {
208             RLOGD("could not open /proc/cmdline:%s", strerror(errno));
209             goto OpenLib;
210         }
211
212         do {
213             len = read(fd,buffer,sizeof(buffer)); }
214         while (len == -1 && errno == EINTR);
215
216         if (len < 0) {
217             RLOGD("could not read /proc/cmdline:%s", strerror(errno));
218             close(fd);
219             goto OpenLib;
220         }
221         close(fd);
222
223         if (strstr(buffer, "android.qemud=") != NULL)
224         {
225             /* the qemud daemon is launched after rild, so
226             * give it some time to create its GSM socket
227             */
228             int  tries = 5;
229 #define  QEMUD_SOCKET_NAME    "qemud"
230
231             while (1) {
232                 int  fd;
233
234                 sleep(1);
235
236                 fd = qemu_pipe_open("qemud:gsm");
237                 if (fd < 0) {
238                     fd = socket_local_client(
239                                 QEMUD_SOCKET_NAME,
240                                 ANDROID_SOCKET_NAMESPACE_RESERVED,
241                                 SOCK_STREAM );
242                 }
243                 if (fd >= 0) {
244                     close(fd);
245                     snprintf( arg_device, sizeof(arg_device), "%s/%s",
246                                 ANDROID_SOCKET_DIR, QEMUD_SOCKET_NAME );
247
248                     arg_overrides[1] = "-s";
249                     arg_overrides[2] = arg_device;
250                     done = 1;
251                     break;
252                 }
253                 RLOGD("could not connect to %s socket: %s",
254                     QEMUD_SOCKET_NAME, strerror(errno));
255                 if (--tries == 0)
256                     break;
257             }
258             if (!done) {
259                 RLOGE("could not connect to %s socket (giving up): %s",
260                     QEMUD_SOCKET_NAME, strerror(errno));
261                 while(1)
262                     sleep(0x00ffffff);
263             }
264         }
265
266         /* otherwise, try to see if we passed a device name from the kernel */
267         if (!done) do {
268 #define  KERNEL_OPTION  "android.ril="
269 #define  DEV_PREFIX     "/dev/"
270
271             p = strstr( buffer, KERNEL_OPTION );
272             if (p == NULL)
273                 break;
274
275             p += sizeof(KERNEL_OPTION)-1;
276             q  = strpbrk( p, " \t\n\r" );
277             if (q != NULL)
278                 *q = 0;
279
280             snprintf( arg_device, sizeof(arg_device), DEV_PREFIX "%s", p );
281             arg_device[sizeof(arg_device)-1] = 0;
282             arg_overrides[1] = "-d";
283             arg_overrides[2] = arg_device;
284             done = 1;
285
286         } while (0);
287
288         if (done) {
289             argv = arg_overrides;
290             argc = 3;
291             i    = 1;
292             hasLibArgs = 1;
293             rilLibPath = REFERENCE_RIL_PATH;
294
295             RLOGD("overriding with %s %s", arg_overrides[1], arg_overrides[2]);
296         }
297     }
298 OpenLib:
299 #endif
300     switchUser();
301
302     dlHandle = dlopen(rilLibPath, RTLD_NOW);
303
304     if (dlHandle == NULL) {
305         RLOGE("dlopen failed: %s", dlerror());
306         exit(EXIT_FAILURE);
307     }
308
309     RIL_startEventLoop();
310
311     rilInit =
312         (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
313         dlsym(dlHandle, "RIL_Init");
314
315     if (rilInit == NULL) {
316         RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath);
317         exit(EXIT_FAILURE);
318     }
319
320     dlerror(); // Clear any previous dlerror
321     rilUimInit =
322         (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
323         dlsym(dlHandle, "RIL_SAP_Init");
324     err_str = dlerror();
325     if (err_str) {
326         RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str);
327     } else if (!rilUimInit) {
328         RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath);
329     }
330
331     if (hasLibArgs) {
332         rilArgv = argv + i - 1;
333         argc = argc -i + 1;
334     } else {
335         static char * newArgv[MAX_LIB_ARGS];
336         static char args[PROPERTY_VALUE_MAX];
337         rilArgv = newArgv;
338         property_get(LIB_ARGS_PROPERTY, args, "");
339         argc = make_argv(args, rilArgv);
340     }
341
342     rilArgv[argc++] = "-c";
343     rilArgv[argc++] = clientId;
344     RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]);
345
346     // Make sure there's a reasonable argv[0]
347     rilArgv[0] = argv[0];
348
349     funcs = rilInit(&s_rilEnv, argc, rilArgv);
350     RLOGD("RIL_Init rilInit completed");
351
352     RIL_register(funcs);
353
354     RLOGD("RIL_Init RIL_register completed");
355
356     if (rilUimInit) {
357         RLOGD("RIL_register_socket started");
358         RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv);
359     }
360
361     RLOGD("RIL_register_socket completed");
362
363 done:
364
365     RLOGD("RIL_Init starting sleep loop");
366     while (true) {
367         sleep(UINT32_MAX);
368     }
369 }