1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
|
/*
* DA manager
*
* Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
*
* Contact:
*
* Jaewon Lim <jaewon81.lim@samsung.com>
* Woojin Jung <woojin2.jung@samsung.com>
* Juyoung Kim <j0.kim@samsung.com>
* Cherepanov Vitaliy <v.cherepanov@samsung.com>
* Nikita Kalyazin <n.kalyazin@samsung.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* - S-Core Co., Ltd
* - Samsung RnD Institute Russia
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h> // for unlink
#include <dirent.h> // for opendir, readdir
#include <sys/types.h> // for open
#include <sys/stat.h> // for open
#include <fcntl.h> // for open
#include <grp.h> // for setgroups
#ifndef LOCALTEST
#include <sys/smack.h>
#endif
#include "daemon.h"
#include "utils.h"
#include "debug.h"
#define BUFFER_MAX 1024
#define APP_GROUPS_MAX 100
#define APP_GROUP_LIST "/usr/share/privilege-control/app_group_list"
#define SELF_LABEL_FILE "/proc/self/attr/current"
#ifndef LOCALTEST
#define SMACK_LABEL_LEN 255
#endif
#define SID_APP 5000
#define MANIFEST_PATH "/info/manifest.xml"
#define APPDIR1 "/opt/apps/"
#define APPDIR2 "/opt/usr/apps/"
uint64_t str_to_uint64(char* str)
{
uint64_t res = 0;
if(str != NULL)
{
while(*str >= '0' && *str <= '9')
{
res = res * 10 + (*str - '0');
str++;
}
}
return res;
}
int64_t str_to_int64(char* str)
{
int64_t res = 0;
int64_t factor = 1;
if(str != NULL)
{
if(*str == '-')
{
factor = -1;
str++;
}
else if(*str == '+')
{
factor = 1;
str++;
}
while(*str >= '0' && *str <= '9')
{
res = res * 10 + (*str - '0');
str++;
}
}
return (res * factor);
}
int read_line(const int fd, char* ptr, const unsigned int maxlen)
{
unsigned int n = 0;
char c[2];
int rc;
while(n != maxlen)
{
if((rc = read(fd, c, 1)) != 1)
return -1; // eof or read err
if(*c == '\n')
{
ptr[n] = 0;
return n;
}
ptr[n++] = *c;
}
return -1; // no space
}
#ifndef LOCALTEST
int smack_set_label_for_self(const char *label)
{
int len;
int fd;
int ret;
len = strnlen(label, SMACK_LABEL_LEN + 1);
if (len > SMACK_LABEL_LEN)
return -1;
fd = open(SELF_LABEL_FILE, O_WRONLY);
if (fd < 0)
return -1;
ret = write(fd, label, len);
close(fd);
return (ret < 0) ? -1 : 0;
}
#endif
void set_appuser_groups(void)
{
int fd = 0;
char buffer[5];
gid_t t_gid = -1;
gid_t groups[APP_GROUPS_MAX] = {0, };
int cnt = 0;
// groups[cnt++] = SID_DEVELOPER;
fd = open(APP_GROUP_LIST, O_RDONLY);
if(fd < 0)
{
LOGE("cannot get app's group lists from %s", APP_GROUP_LIST);
return;
}
for(;;)
{
if(read_line(fd, buffer, sizeof buffer) < 0)
{
break;
}
t_gid = strtoul(buffer, 0, 10);
errno = 0;
if(errno != 0)
{
LOGE("cannot change string to integer: [%s]\n", buffer);
continue;
}
if(t_gid)
{
if(cnt < APP_GROUPS_MAX)
{
groups[cnt++] = t_gid;
}
else
{
LOGE("cannot add groups more than %d", APP_GROUPS_MAX);
break;
}
}
}
if(cnt > 0)
{
if(setgroups(sizeof(groups) / sizeof(groups[0]), groups) != 0)
{
fprintf(stderr, "set groups failed errno: %d\n", errno);
close(fd);
exit(1);
}
}
close(fd);
}
#ifndef LOCALTEST
int get_smack_label(const char* execpath, char* buffer, int buflen)
{
char* appid = NULL;
int rc;
rc = smack_lgetlabel(execpath, &appid, SMACK_LABEL_ACCESS);
if(rc == 0 && appid != NULL)
{
strncpy(buffer, appid, (buflen < strlen(appid))? buflen:strlen(appid));
free(appid);
return 0;
}
else
return -1;
}
#endif
int get_appid(const char* execpath, char* buffer, int buflen)
{
int i, ret = 0;
char* temp;
if(strncmp(execpath, APPDIR1, strlen(APPDIR1)) == 0)
{
execpath = execpath + strlen(APPDIR1);
temp = strchr(execpath, '/');
for(i = 0; i < strlen(execpath); i++)
{
if(execpath + i == temp)
break;
buffer[i] = execpath[i];
}
buffer[i] = '.';
buffer[i+1] = '\0';
temp = strrchr(execpath, '/');
if(temp != NULL)
{
temp++;
strcat(buffer, temp);
}
}
else if(strncmp(execpath, APPDIR2, strlen(APPDIR2)) == 0)
{
execpath = execpath + strlen(APPDIR2);
temp = strchr(execpath, '/');
for(i = 0; i < strlen(execpath); i++)
{
if(execpath + i == temp)
break;
buffer[i] = execpath[i];
}
buffer[i] = '.';
buffer[i+1] = '\0';
temp = strrchr(execpath, '/');
if(temp != NULL)
{
temp++;
strcat(buffer, temp);
}
}
else
{
ret = -1;
}
return ret;
}
// return 0 if succeed
// return -1 if error occured
int remove_indir(const char *dirname)
{
DIR *dir;
struct dirent *entry;
char path[PATH_MAX];
dir = opendir(dirname);
if(dir == NULL)
{
return -1;
}
while((entry = readdir(dir)) != NULL)
{
if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
{
snprintf(path, (size_t) PATH_MAX, "%s/%s", dirname, entry->d_name);
if (entry->d_type != DT_DIR) // file
{
unlink(path);
}
else { } // directory
}
}
closedir(dir);
return 0;
}
// get application name from executable binary path
char* get_app_name(char* binary_path)
{
char* pos;
pos = strrchr(binary_path, '/');
if(pos != NULL)
pos++;
return pos;
}
// return 0 to normal case
// return non-zero to error case
int get_manifest_path(const char* exec_path, char* buf, int buflen)
{
char* chr;
strcpy(buf, exec_path);
chr = strrchr(buf, '/');
if(chr == NULL)
return -1;
*chr = '\0';
chr = strrchr(buf, '/');
if(chr == NULL)
return -1;
*chr = '\0';
strcat(buf, MANIFEST_PATH);
return 0;
}
// execute applcation with executable binary path
// return 0 to fail to execute
// return 1 to succeed to execute
int exec_app_tizen(const char *app_id, const char *exec_path)
{
LOGI("exec %s\n", exec_path);
pid_t pid;
//char command[PATH_MAX];
//char appid[PATH_MAX];
if (exec_path == NULL || !strlen(exec_path)) {
LOGE("Executable path is not correct\n");
return 0;
}
pid = fork();
if (pid == -1)
return 1;
else if (pid > 0)
return 0; // exit parent process with success
LOGI("launch app path is %s, executable path is %s\n"
"launch app name (%s), app_id (%s)\n",
LAUNCH_APP_PATH, exec_path,
LAUNCH_APP_NAME, app_id);
execl(LAUNCH_APP_PATH, LAUNCH_APP_NAME, app_id, LAUNCH_APP_SDK,
DA_PRELOAD_EXEC, NULL);
return 0;
}
int exec_app_common(const char* exec_path)
{
LOGI("kill %s\n", exec_path);
pid_t pid;
int hw_acc = 0;
char manifest[PATH_MAX];
char command[PATH_MAX];
#ifndef LOCALTEST
// TODO: check if this makes sense
/* char appid[SMACK_LABEL_LEN]; */
#endif
if (exec_path == NULL || !strlen(exec_path)) {
LOGE("Executable path is not correct\n");
return 1;
}
pid = fork();
if (pid == -1)
return 1;
else if (pid > 0)
return 0; // exit parent process with success
if (get_manifest_path(exec_path, manifest, PATH_MAX) == 0) {
FILE* fp;
char buffer[BUFFER_MAX];
char* res;
// grep for manifest
sprintf(command, "grep \"HwAcceleration=\\\"On\\\"\" %s",
manifest);
fp = popen(command, "r");
if (fp != NULL) {
buffer[0] = '\0';
res = fgets(buffer, BUFFER_MAX, fp);
if (res != NULL && strlen(buffer) != 0) {
hw_acc = 1;
}
pclose(fp);
}
}
// FIXME: I think the followin does not make sense
/* #ifndef LOCALTEST */
/* if (get_smack_label(exec_path, appid, SMACK_LABEL_LEN - 1) < 0) { */
/* LOGE("failed to get smack label\n"); */
/* return 1; */
/* } else */
/* #endif */
/* { */
/* #ifndef LOCALTEST */
/* LOGI("smack lable is %s\n", appid); */
/* if(smack_set_label_for_self(appid) < 0) { */
/* LOGE("failed to set label for self\n"); */
/* } */
/* #endif */
/* set_appuser_groups(); */
/* if (setgid(SID_APP) < 0) { */
/* LOGE("failed to setgid\n"); */
/* } */
/* if (setuid(SID_APP) < 0) { */
/* LOGE("failed to setuid\n"); */
/* } */
/* pid = getpid(); */
/* if (setpgid(pid, pid) < 0) { */
/* LOGE("failed to setpgid\n"); */
/* } */
/* if (hw_acc != 0) { */
/* sprintf(command, "HWACC=USE %s %s", DA_PRELOAD(app_type), exec_path); */
/* } else { */
/* sprintf(command, "%s %s", DA_PRELOAD(app_type), exec_path); */
/* } */
/* LOGI("launch app path is %s, executable path is %s\n", LAUNCH_APP_PATH, exec_path); */
/* execl(SHELL_CMD, SHELL_CMD, "-c", command, NULL); */
/* return 0; */
/* } */
// TODO:
/* if (hw_acc) */
/* sprintf(command, "HWACC=USE %s %s", */
/* DA_PRELOAD(app_type), exec_path); */
/* else */
/* sprintf(command, "%s %s", */
/* DA_PRELOAD(app_type), exec_path); */
#ifndef LOCALTEST
sprintf(command, "%s %s",
DA_PRELOAD_OSP, exec_path);
#else /* LOCALTEST */
sprintf(command, "%s", exec_path);
#endif /* LOCALTEST */
LOGI("cmd: %s\n", command);
execl(SHELL_CMD, SHELL_CMD, "-c", command, NULL);
return 0;
}
// find process id from executable binary path
pid_t find_pid_from_path(const char* path)
{
pid_t status = -1;
char buffer [BUFFER_MAX];
char command [BUFFER_MAX];
sprintf(command, "/bin/pidof %s", path);
FILE *fp = popen(command, "r");
if (!fp)
{
LOGE("Getting pidof %s is failed\n", path);
return status;
}
while (fgets(buffer, BUFFER_MAX, fp) != NULL)
{
LOGI("result of 'pidof' is %s\n", buffer);
}
pclose(fp);
if (strlen(buffer) > 0)
{
if (sscanf(buffer,"%d\n", &status) != 1)
{
LOGE("Failed to read result buffer of 'pidof', status(%d)\n", status);
return -1;
}
}
return status;
}
void kill_app(const char* binary_path)
{
LOGI("kill %s\n", binary_path);
pid_t pkg_pid;
char command[PATH_MAX];
pkg_pid = find_pid_from_path(binary_path);
if(pkg_pid > 0)
{
sprintf(command, "kill -9 %d", pkg_pid);
system(command);
}
}
int get_executable(char* appPath, char* buf, int buflen)
{
int fd;
sprintf(buf, "%s.exe", appPath);
fd = open(buf, O_RDONLY);
if(fd != -1)
{
close(fd);
}
else
{
strcpy(buf, appPath);
}
return 0;
}
int get_app_install_path(char *strAppInstall, int length)
{
FILE *fp;
char buf[BUFFER_MAX];
char *p;
int i;
if ((fp = fopen(DA_INSTALL_PATH, "r")) == NULL)
{
LOGE("Failed to open %s\n", DA_INSTALL_PATH);
return -1;
}
/*ex : <15> DW_AT_comp_dir : (indirect string, offset: 0x25f): /home/yt/workspace/templatetest/Debug-Tizen-Emulator */
while (fgets(buf, BUFFER_MAX, fp) != NULL)
{
//name
p = buf;
for (i = 0; i < BUFFER_MAX; i++)
{
if (*p == ':')
break;
p++;
}
if (*p != ':')
break;
else
p++;
//(...,offset:...)
for (; i < BUFFER_MAX; i++)
{
if (*p == '(')
{
while (*p != ')')
{
p++;
}
}
if (*p == ':')
break;
p++;
}
//find
if (*p != ':')
break;
for (; i < BUFFER_MAX; i++)
{
if (*p == ':' || *p == ' ' || *p == '\t')
p++;
else
break;
}
//name
if (strlen(p) <= length)
{
sprintf(strAppInstall, "%s", p);
for (i = 0; i < strlen(p); i++)
{
if (strAppInstall[i] == '\n' || strAppInstall[i] == '\t')
{
strAppInstall[i] = '\0';
break;
}
}
fclose(fp);
return 1;
}
}
fclose(fp);
return -1;
}
int is_app_built_pie(void)
{
int result;
FILE *fp;
char buf[BUFFER_MAX];
if((fp = fopen(DA_BUILD_OPTION, "r")) == NULL)
{
LOGE("Failed to open %s\n", DA_BUILD_OPTION);
return -1;
}
if(fgets(buf, BUFFER_MAX, fp) != NULL)
{
if(strcmp(buf, "DYN\n") == 0)
result = 1;
else if(strcmp(buf, "EXEC\n") == 0)
result = 0;
else
result = -1;
}
else
{
result = -1;
}
fclose(fp);
return result;
}
int get_app_base_address(int *baseAddress)
{
int res;
FILE *fp;
char buf[BUFFER_MAX];
if((fp = fopen(DA_BASE_ADDRESS, "r")) == NULL)
{
LOGE("Failed to open %s\n", DA_BASE_ADDRESS);
return -1;
}
if(fgets(buf, BUFFER_MAX, fp) != NULL)
{
res = sscanf(buf, "%x", baseAddress);
}
else
{
res = -1;
}
fclose(fp);
return res;
}
int is_same_app_process(char* appPath, int pid)
{
int ret = 0;
FILE *fp;
char buf[BUFFER_MAX];
char cmdPath[PATH_MAX];
char execPath[PATH_MAX];
get_executable(appPath, execPath, PATH_MAX);
sprintf(cmdPath, "/proc/%d/cmdline", pid);
if((fp = fopen(cmdPath, "r")) == NULL)
{
return 0;
}
if(fgets(buf, BUFFER_MAX, fp) != NULL)
{
#ifdef RUN_APP_LOADER
if(strcmp(buf, appPath) == 0)
#else
// use execPath instead of manager.appPath
if(strcmp(buf, execPath) == 0)
#endif
ret = 1;
else
ret = 0;
}
fclose(fp);
return ret;
}
|