summaryrefstree
path: root/kernel/security/sdp/fs_handler.c
blob: 6e711132c0d2063cd2c729d8212efad4a32f568b (plain)
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
/*
 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
 *
 * Sensitive Data Protection
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <crypto/internal/hash.h>
#include <crypto/scatterwalk.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
#include <linux/interrupt.h>
#include <linux/wakelock.h>
#include <linux/sched.h>
#include <linux/netlink.h>
#include <linux/net.h>
#include <net/netlink.h>
#include <net/sock.h>
#include <net/net_namespace.h>
#include <linux/types.h>
#include <linux/wait.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/audit.h>
#include <linux/jiffies.h>

#include <linux/version.h>
#include <sdp/fs_handler.h>
#include <sdp/fs_request.h>

#define RESULT_ARRAY_MAX_LEN 100

#define CRYPTO_MAX_TIMEOUT HZ/5

#define SDP_FS_HANDLER_REQ_TIMEOUT 3000

sdp_fs_handler_control_t g_sdp_fs_handler_control;

DEFINE_MUTEX(g_send_mutex);
static int g_user_pid = 0;
static struct sock* g_sock = NULL;

static int to_netlink_msg(sdp_fs_handler_request_t *req, char **msg);
static void request_send(sdp_fs_handler_control_t *con,
        sdp_fs_handler_request_t *req);
static sdp_fs_handler_request_t *request_find(sdp_fs_handler_control_t *con,
        u32 request_id);
static sdp_fs_handler_request_t *request_alloc(u32 opcode);
static void request_free(sdp_fs_handler_request_t *req);
static void req_dump(sdp_fs_handler_request_t *req, const char *msg);

/* Debug */
#define SDP_FS_HANDLER_DEBUG       0

#if SDP_FS_HANDLER_DEBUG
#define SDP_FS_HANDLER_LOGD(FMT, ...) printk("SDP_FS_HANDLER[%d] : %s " FMT , current->pid, __func__, ##__VA_ARGS__)
#else
#define SDP_FS_HANDLER_LOGD(FMT, ...)
#endif /* SDP_FS_HANDLER_DEBUG */
#define SDP_FS_HANDLER_LOGE(FMT, ...) printk("SDP_FS_HANDLER[%d]  : %s " FMT , current->pid, __func__, ##__VA_ARGS__)

static int __handle_request(sdp_fs_handler_request_t *req, char *ret) {
    int rc = 0;

    struct sk_buff *skb_in = NULL;
    struct sk_buff *skb_out = NULL;
    struct nlmsghdr *nlh = NULL;

    char *nl_msg = NULL;
    int nl_msg_size = 0;

    SDP_FS_HANDLER_LOGD("====================== \t entred\n");

    if(req == NULL) {
        SDP_FS_HANDLER_LOGE("invalid request\n");
        return -1;
    }

    request_send(&g_sdp_fs_handler_control, req);

    nl_msg_size = to_netlink_msg(req, &nl_msg);
    if(nl_msg_size <= 0) {
        SDP_FS_HANDLER_LOGE("invalid opcode %d\n", req->opcode);
        return -1;
    }

    // sending netlink message
    skb_in = nlmsg_new(nl_msg_size, 0);
    if (!skb_in) {
        SDP_FS_HANDLER_LOGE("Failed to allocate new skb: \n");
        return -1;
    }

    nlh = nlmsg_put(skb_in, 0, 0, NLMSG_DONE, nl_msg_size, 0);
    NETLINK_CB(skb_in).dst_group = 0;
    if(nlh != NULL) {
        memcpy(nlmsg_data(nlh), nl_msg, nl_msg_size);
    }

    mutex_lock(&g_send_mutex);
    rc = nlmsg_unicast(g_sock, skb_in, g_user_pid);
    mutex_unlock(&g_send_mutex);

    skb_out = skb_dequeue(&g_sock->sk_receive_queue);
    if(skb_out) {
        kfree_skb(skb_out);
    }
    return 0;

}

int sdp_fs_request(sdp_fs_command_t *cmd, fs_request_cb_t callback){
    sdp_fs_handler_request_t *req = request_alloc(cmd->opcode);
    int ret = -1;
    req_dump(req, "request allocated");

    if(req) {
    	memcpy(&req->command, cmd, sizeof(sdp_fs_command_t));
    	req->command.req_id = req->id;

        req_dump(req, "__handle_reqeust start");
        ret = __handle_request(req, NULL);
        req_dump(req, "__handle_reqeust end");

        if(ret != 0) {
            SDP_FS_HANDLER_LOGE("opcode[%d] failed\n", cmd->opcode);
            goto error;
        }
    } else {
        SDP_FS_HANDLER_LOGE("request allocation failed\n");
        return -ENOMEM;
    }

    return 0;
    error:
    request_free(req);
    return -1;
}

static int __recver(struct sk_buff *skb, struct nlmsghdr *nlh)
{
    void            *data;
    u16         msg_type = nlh->nlmsg_type;
    u32 err = 0;
    struct audit_status *status_get = NULL;
    u16 len = 0;

    data = NLMSG_DATA(nlh);
    len = ntohs(*(uint16_t*) (data+1));
    switch (msg_type) {
    case SDP_FS_HANDLER_PID_SET:
        status_get   = (struct audit_status *)data;
        g_user_pid = status_get->pid;
        break;
    case SDP_FS_HANDLER_RESULT:
    {
        result_t *result = (result_t *)data;
        sdp_fs_handler_request_t *req = NULL;

        printk("result : req_id[%d], opcode[%d] ret[%d]\n",
                result->request_id, result->opcode, result->ret);
        spin_lock(&g_sdp_fs_handler_control.lock);
        req = request_find(&g_sdp_fs_handler_control, result->request_id);
        spin_unlock(&g_sdp_fs_handler_control.lock);

        if(req == NULL) {
            SDP_FS_HANDLER_LOGE("crypto result :: error! can't find request %d\n",
                    result->request_id);
        } else {
            memcpy(&req->result, result, sizeof(result_t));
            req->state = SDP_FS_HANDLER_REQ_FINISHED;

            if(req->callback)
                req->callback(req->opcode, req->result.ret, req->command.ino);

            memset(result, 0, sizeof(result_t));
            request_free(req);
        }
        break;
    }
    default:
        SDP_FS_HANDLER_LOGE("unknown message type : %d\n", msg_type);
        break;
    }

    return err;
}

/* Receive messages from netlink socket. */
static void recver(struct sk_buff  *skb)
{
    struct nlmsghdr *nlh;
    int len;
    int err;

    nlh = nlmsg_hdr(skb);
    len = skb->len;

    err = __recver(skb, nlh);
}

static int to_netlink_msg(sdp_fs_handler_request_t *req, char **msg)
{
	*msg = (char *)&req->command;
	return sizeof(sdp_fs_command_t);
}

static u32 get_unique_id(sdp_fs_handler_control_t *control)
{
    SDP_FS_HANDLER_LOGD("locked\n");
    spin_lock(&control->lock);

    control->reqctr++;
    /* zero is special */
    if (control->reqctr == 0)
        control->reqctr = 1;

    spin_unlock(&control->lock);
    SDP_FS_HANDLER_LOGD("unlocked\n");

    return control->reqctr;
}
static void req_dump(sdp_fs_handler_request_t *req, const char *msg) {
#if SDP_FS_HANDLER_DEBUG
    SDP_FS_HANDLER_LOGD("DUMP REQUEST [%s] ID[%d] opcode[%d] state[%d]\n", msg, req->id, req->opcode, req->state);
#endif
}

static void request_send(sdp_fs_handler_control_t *con,
        sdp_fs_handler_request_t *req) {
    spin_lock(&con->lock);
    SDP_FS_HANDLER_LOGD("entered, control lock\n");

    list_add_tail(&req->list, &con->pending_list);
    req->state = SDP_FS_HANDLER_REQ_PENDING;

    SDP_FS_HANDLER_LOGD("exit, control unlock\n");
    spin_unlock(&con->lock);
}

static sdp_fs_handler_request_t *request_find(sdp_fs_handler_control_t *con,
        u32 request_id) {
    struct list_head *entry;

    list_for_each(entry, &con->pending_list) {
        sdp_fs_handler_request_t *req;
        req = list_entry(entry, sdp_fs_handler_request_t, list);
        if (req->id == request_id)
            return req;
    }

    return NULL;
}

static struct kmem_cache *req_cachep;

static void request_init(sdp_fs_handler_request_t *req, u32 opcode) {
    memset(req, 0, sizeof(sdp_fs_handler_request_t));

    req->state = SDP_FS_HANDLER_REQ_INIT;
    req->id = get_unique_id(&g_sdp_fs_handler_control);

    INIT_LIST_HEAD(&req->list);
    atomic_set(&req->count, 1);
    req->aborted = 0;
    req->opcode = opcode;
    req->callback = NULL;
}

static sdp_fs_handler_request_t *request_alloc(u32 opcode) {
    sdp_fs_handler_request_t *req = kmem_cache_alloc(req_cachep, GFP_KERNEL);

    if(req)
        request_init(req, opcode);
    return req;
}

static void request_free(sdp_fs_handler_request_t *req)
{
    if(req) {
        req_dump(req, "request freed");
        /*
         * TODO : lock needed here?
         */
        list_del(&req->list);
        memset(req, 0, sizeof(sdp_fs_handler_request_t));
        kmem_cache_free(req_cachep, req);
    } else {
        SDP_FS_HANDLER_LOGE("req is NULL, skip free\n");
    }
}

static void control_init(sdp_fs_handler_control_t *con) {
    SDP_FS_HANDLER_LOGD("sdp_fs_handler_control_init");
    spin_lock_init(&con->lock);
    INIT_LIST_HEAD(&con->pending_list);

    spin_lock(&con->lock);
    con->reqctr = 0;
    spin_unlock(&con->lock);
}

static int __init sdp_fs_handler_mod_init(void) {
#if (LINUX_VERSION_CODE > KERNEL_VERSION(3,4,0))
    struct netlink_kernel_cfg cfg = {
            .input  = recver,
    };

    g_sock = netlink_kernel_create(&init_net, SDP_FS_HANDLER_NETLINK, &cfg);
#else
    g_sock = netlink_kernel_create(&init_net, SDP_FS_HANDLER_NETLINK, 0, recver, NULL, THIS_MODULE);
#endif

    if (!g_sock) {
        SDP_FS_HANDLER_LOGE("Failed to create Crypto Netlink Socket .. Exiting \n");
        return -ENOMEM;
    }
    SDP_FS_HANDLER_LOGE("netlink socket is created successfully! \n");

    control_init(&g_sdp_fs_handler_control);
    req_cachep = kmem_cache_create("sdp_fs_handler_requst",
            sizeof(sdp_fs_handler_request_t),
            0, 0, NULL);
    if (!req_cachep) {
        netlink_kernel_release(g_sock);
        SDP_FS_HANDLER_LOGE("Failed to create sdp_fs_handler_requst cache mem.. Exiting \n");
        return -ENOMEM;
    }

    return 0;
}

static void __exit sdp_fs_handler_mod_exit(void) {
    netlink_kernel_release(g_sock);
    kmem_cache_destroy(req_cachep);
}

module_init(sdp_fs_handler_mod_init);
module_exit(sdp_fs_handler_mod_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("SDP FS netlink");