aboutsummaryrefslogtreecommitdiffstats
path: root/src/floader/loader.c
blob: 0970794e9fe660ab397f91e834b4218261422987 (plain) (blame)
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
/*
 * Copyright (C) 2017 Madura A. (madura.x86@gmail.com)
 * 
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
    
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define MAX_MATCHES 32
#define MAX_ARGS 1024
#define MAX_ARGS_LEN 4096
static void loader_main() __attribute__((constructor));

char cmdline[MAX_ARGS_LEN];
char *args[MAX_ARGS];
char loader[] = "firejail";
char confFile[256];
char *names[MAX_MATCHES];

#ifdef DEBUG
#define DBG printf
#else
#define DBG
#endif
void remove_trailing_spaces(char *str)
{
    while (!isspace(*str))
    {
        str++;
    }
    
    while (*str != '\0')
    {
        *str = '\0';
        str++;
    }
}

void read_cmdline()
{
    int fd = open("/proc/self/cmdline", O_RDONLY);
    ssize_t ret = 0, total = 0;
    char* wcmdbuf = cmdline;
    while ((ret = read(fd, wcmdbuf, 1)) != 0)
    {
        wcmdbuf++;
        total += ret;
        if (total > MAX_ARGS_LEN)
        {
            printf("Not enough memory\n");
            close(fd);
            return ;
        }
    }
    close(fd);
}

void make_args()
{
    int cI = 0, argI=0;
    char* argstart = &cmdline[0];
    for (;cI<MAX_ARGS_LEN;cI++)
    {
        if (cmdline[cI] == '\0')
        {
            args[argI]= argstart; 
            argstart = &cmdline[cI+1];
            argI++;
            if (*argstart  == '\0')
            {
                break;
            }
        }
    }
    args[argI] = argstart;
    argI++;
    args[argI] = NULL;
}

void loader_main()
{
    snprintf(confFile, 255, "%s/.loader.conf", getenv("HOME"));

    struct stat confFileStat;
    
    stat(confFile, &confFileStat);
    
    int confFd = open(confFile, O_RDONLY);
    
    if (confFd == -1)
    {
        close(confFd);
        return;
    }
    char* conf = (char*) malloc(confFileStat.st_size);
    if (conf == NULL)
    {
        close(confFd);
        return;
    }
    ssize_t ret  = read(confFd, conf, confFileStat.st_size);
    if (ret == -1)
    {
        close(confFd);
        return;
    }
    
    close(confFd);
    size_t fI = 0;
    int matchId = 0;
    names[matchId] = conf;
    matchId++;
    for (;fI < confFileStat.st_size-1;fI++)
    {
        if (conf[fI] == ',')
        {
            names[matchId] = &conf[fI+1];
            conf[fI] = '\0';
            
            matchId++;
        }
    }
       
    remove_trailing_spaces(names[matchId-1]);
   
    read_cmdline();
    
    make_args();
    
#ifdef DEBUG
    int xarg=0;
    while (args[xarg] != NULL)
    {
        DBG(".%s\n", args[xarg]);
        xarg++;
    }
#endif

    int x;
       
    for (x = 0;x<matchId;x++)
    {
        DBG("%s\n",names[x]);
        if (strstr(args[0], names[x]) != NULL)
        {
            DBG("highjack!\n");
            
            free(conf);
            
            execvp(loader, args );
        }
    }
    
}