summaryrefslogtreecommitdiff
path: root/drivers/staging/tuxguardian/tg.c
blob: 40d4c151a80e76a1a63d1285e899bdf2278179f7 (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
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
/*
    TuxGuardian is copyright 2004, Bruno Castro da Silva (brunocs@portoweb.com.br)
                                   http://tuxguardian.sourceforge.net

    This file is part of TuxGuardian.

    TuxGuardian is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    TuxGuardian 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 TuxGuardian; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
*/




#include "tg.h"
#include <linux/ip.h>
#include <linux/version.h>

#ifndef KERNEL_VERSION
#define KERNEL_VERSION(a,b,c) (((a) << 16) | ((b) << 8) | (c))
#endif

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
#include <net/inet_sock.h>
#endif



int send_question_permit(struct socket **sock, pid_t pid, int question);
extern int is_internet_socket(int family);
extern void print_socket_info(int family, int type);
extern int read_answer_from_daemon(struct socket *sock, struct tg_query *answer);

//--------------------------------------------------------------------------------
static int tuxguardian_bprm_check_security (struct linux_binprm *bprm)
{
/*  binprm_security_ops is a set of program loading hook */
/*   debug_message("file %s, e_uid = %d, e_gid = %d\n", */
/* 		bprm->filename, bprm->e_uid, bprm->e_gid); */
  return 0;
}
//--------------------------------------------------------------------------------



//--------------------------------------------------------------------------------
static int tuxguardian_socket_create(int family, int type, int protocol, int kern)
{
  // TODO: i think the 'kern' parameter is used to inform if the socket
  // was created in kernel space.. not using this parameter, though

  int retval;
  struct tg_query answer;
  struct socket *sock;


  // local communication is always allowed
  // (notice that since we are creating local sockets to
  //   communicate w/ userspace this function SHOULD NOT
  //   analyse local sockets or it'll loop)

  if (! is_internet_socket(family)) {
//    printk("local communication always allowed\n");
    return 0;
  }
/*   else { */
/*     printk("\nAsking if '%s' may create a socket (", current->comm); */
/*   print_socket_info(family, type); */
/*     printk(")\n"); */
/*   } */


  retval = send_question_permit(&sock, current->pid, TG_ASK_PERMIT_APP);
  if (retval < 0)
    return -EPERM;

  // TODO: controle de timeout
  // /usr/src/linux269/fs/ncpfs/sock.c
  // /usr/src/linux269/net/bluetooth/rfcomm/sock.c
  // sock_rcvtimeo
  retval = read_answer_from_daemon(sock, &answer);
  if (retval < 0)
    return -EPERM;


  switch (answer.query_data) {
  case YES:
    return 0;
  case NO_ACCESS_IS_DENIED:
    printk(KERN_INFO "TuxGuardian: process #%d (%s) will not be allowed to access the internet"
	   " (ACCESS IS DENIED)\n", current->pid, current->comm);
    return -EPERM;
  case NO_WRONG_HASH:
    printk(KERN_INFO "TuxGuardian: process #%d (%s) will not be allowed to access the internet"
	   " (WRONG MD5HASH)\n", current->pid, current->comm);
    return -EPERM;
  case NO_NOT_IN_HASHTABLE:
    printk(KERN_INFO "TuxGuardian: process #%d (%s) will not be allowed to access the internet"
	   " (APP NOT PERMITTED)\n", current->pid, current->comm);
    return -EPERM;
  case NO_ERROR_IN_DAEMON:
    printk(KERN_INFO "TuxGuardian: process #%d (%s) will not be allowed to access the internet"
	   " (ERR IN DAEMON)\n", current->pid, current->comm);
    return -EPERM;
  case NO_USER_FORBID:
    printk(KERN_INFO "TuxGuardian: process #%d (%s) will not be allowed to access the internet"
	   " (USER FORBID)\n", current->pid, current->comm);
    return -EPERM;
  case NO_ERROR_IN_FRONTEND:
    printk(KERN_INFO "TuxGuardian: process #%d (%s) will not be allowed to access the internet"
	   " (ERR IN FRONTEND)\n", current->pid, current->comm);
    return -EPERM;
  default:
    printk(KERN_INFO "TuxGuardian: process #%d (%s) will not be allowed to access the internet"
	   " (UNKNOWN ANSWER %d)\n", current->pid, current->comm, answer.query_data);
    return -EPERM;
  }


/*   printk("resposta -> sender: %d   seqno: %d    query_type: %d    query_data:  %d\n", */
/* 	 (int)answer.sender, answer.seqno, (int)answer.query_type, answer.query_data); */

  // connection is closed by the daemon
  //  sock_release(sock);

}
//--------------------------------------------------------------------------------





//--------------------------------------------------------------------------------
static int tuxguardian_socket_connect(struct socket *conn_sock, struct sockaddr *address, int addrlen)
{
//  printk("tuxguardian_socket_connect!\n");
  return 0;

}
//--------------------------------------------------------------------------------


//--------------------------------------------------------------------------------
static int tuxguardian_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
{
  //  printk("tuxguardian_socket_bind!\n");
  return 0;
}
//--------------------------------------------------------------------------------


//--------------------------------------------------------------------------------
static int tuxguardian_socket_listen(struct socket *listen_sock, int backlog)
{
  int retval;
  struct tg_query answer;
  struct socket *sock;




  // local communication is always allowed
  // (notice that since we are creating local sockets to
  //   communicate w/ userspace this function SHOULD NOT
  //   analyse local sockets or it'll loop)

  if (! is_internet_socket(listen_sock->sk->sk_family)) {
//    printk("local communication always allowed\n");
    return 0;
  }
/*   else { */
/*   printk("\nAsking if '%s' may be a server - ", current->comm); */
/*   print_socket_info(listen_sock->sk->sk_family, listen_sock->sk->sk_type);  */
/*   printk("\n                                   protocol %s at port %d\n", listen_sock->sk->sk_prot->name, */
/* 	 htons(((struct inet_opt *)inet_sk(listen_sock->sk))->sport)); */
/*   } */

  retval = send_question_permit(&sock, current->pid, TG_PERMIT_SERVER);
  if (retval < 0)
    return -EPERM;

  // TODO: timeout
  // /usr/src/linux269/fs/ncpfs/sock.c
  // /usr/src/linux269/net/bluetooth/rfcomm/sock.c
  // sock_rcvtimeo
  retval = read_answer_from_daemon(sock, &answer);
  if (retval < 0)
    return -EPERM;


  switch (answer.query_data) {
  case YES:
    return 0;
  case NO_ACCESS_IS_DENIED:
    printk(KERN_INFO "TuxGuardian: process #%d (%s@%d) will not be allowed to act like a server"
	   " (ACCESS IS DENIED)\n", current->pid, current->comm,
	   htons(((struct inet_sock *)inet_sk(listen_sock->sk))->sport));
    return -EPERM;
  case NO_WRONG_HASH:
    printk(KERN_INFO "TuxGuardian: process #%d (%s@%d) will not be allowed to act like a server"
	   " (WRONG MD5HASH)\n", current->pid, current->comm,
	   htons(((struct inet_sock*)inet_sk(listen_sock->sk))->sport));
    return -EPERM;
  case NO_NOT_IN_HASHTABLE:
    printk(KERN_INFO "TuxGuardian: process #%d (%s@%d) will not be allowed to act like a server"
	   " (APP NOT PERMITTED)\n", current->pid, current->comm,
	   htons(((struct inet_sock *)inet_sk(listen_sock->sk))->sport));
    return -EPERM;
  case NO_ERROR_IN_DAEMON:
    printk(KERN_INFO "TuxGuardian: process #%d (%s@%d) will not be allowed to act like a server"
	   " (ERR IN DAEMON)\n", current->pid, current->comm,
	   htons(((struct inet_sock *)inet_sk(listen_sock->sk))->sport));
    return -EPERM;
  case NO_USER_FORBID:
    printk(KERN_INFO "TuxGuardian: process #%d (%s@%d) will not be allowed to act like a server"
	   " (USER FORBID)\n", current->pid, current->comm,
	   htons(((struct inet_sock *)inet_sk(listen_sock->sk))->sport));
    return -EPERM;
  case NO_ERROR_IN_FRONTEND:
    printk(KERN_INFO "TuxGuardian: process #%d (%s@%d) will not be allowed to act like a server"
	   " (ERR IN FRONTEND)\n", current->pid, current->comm,
	   htons(((struct inet_sock *)inet_sk(listen_sock->sk))->sport));
    return -EPERM;
  default:
    printk(KERN_INFO "TuxGuardian: process #%d (%s@%d) will not be allowed to act like a server"
	   " (UNKNOWN ANSWER %d)\n", current->pid, current->comm, answer.query_data,
	   htons(((struct inet_sock *)inet_sk(listen_sock->sk))->sport));
    return -EPERM;
  }


/*   printk("resposta -> sender: %d   seqno: %d    query_type: %d    query_data:  %d\n", */
/* 	 (int)answer.sender, answer.seqno, (int)answer.query_type, answer.query_data); */

  // connection is closed by the daemon
//  sock_release(sock);
}
//--------------------------------------------------------------------------------



//--------------------------------------------------------------------------------
static int tuxguardian_socket_accept(struct socket *sock, struct socket *newsock)
{
//  printk("tuxguardian_socket_accept!\n");
  return 0;
}
//--------------------------------------------------------------------------------



//--------------------------------------------------------------------------------
static int tuxguardian_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size)
{
  //  printk("tuxguardian_socket_sendmsg!\n");
  return 0;
}
//--------------------------------------------------------------------------------



//--------------------------------------------------------------------------------
static int tuxguardian_socket_recvmsg(struct socket *sock, struct msghdr *msg, int size, int flags)
{
  //  printk("tuxguardian_socket_recvmsg!\n");
  return 0;
}
//--------------------------------------------------------------------------------



//--------------------------------------------------------------------------------
static int tuxguardian_socket_getsockname(struct socket *sock)
{
//  printk("tuxguardian_socket_getsockname!\n");
  return 0;
}
//--------------------------------------------------------------------------------



//--------------------------------------------------------------------------------
static int tuxguardian_socket_getpeername(struct socket *sock)
{
//  printk("tuxguardian_socket_getpeername!\n");
  return 0;
}
//--------------------------------------------------------------------------------



//--------------------------------------------------------------------------------
static int tuxguardian_socket_setsockopt(struct socket *sock,int level,int optname)
{
//  printk("tuxguardian_socket_setsockopt!\n");
  return 0;
}
//--------------------------------------------------------------------------------



//--------------------------------------------------------------------------------
static int tuxguardian_socket_getsockopt(struct socket *sock, int level, int optname)
{
//  printk("tuxguardian_socket_getsockopt!\n");
  return 0;
}
//--------------------------------------------------------------------------------



//--------------------------------------------------------------------------------
static int tuxguardian_socket_shutdown(struct socket *sock, int how)
{
//  printk("tuxguardian_socket_shutdown!\n");
  return 0;
}
//--------------------------------------------------------------------------------



//--------------------------------------------------------------------------------
static int tuxguardian_socket_unix_stream_connect(struct socket *sock,
					      struct socket *other,
					      struct sock *newsk)
{
//  printk("tuxguardian_socket_unix_stream_connect!\n");
  return 0;
}
//--------------------------------------------------------------------------------



//--------------------------------------------------------------------------------
static int tuxguardian_socket_unix_may_send(struct socket *sock,
					struct socket *other)
{
//  printk("tuxguardian_socket_unix_stream_send!\n");
  return 0;
}
//--------------------------------------------------------------------------------










// commoncap.c exporta umas coisa dessas
static struct security_operations tuxguardian_security_ops = {
  /* Use the capability functions for some of the hooks */
  .ptrace                    =  cap_ptrace,
  .capget                    =  cap_capget,
  .capset_check              =	cap_capset_check,
  .capset_set                =  cap_capset_set,
  .capable                   =  cap_capable,

  // evil callback that keeps changing name beetwen kernel releases!
  .bprm_apply_creds          =  cap_bprm_apply_creds,

  .bprm_set_security         =	cap_bprm_set_security,

  .task_post_setuid          =	cap_task_post_setuid,
  .task_reparent_to_init     =	cap_task_reparent_to_init,

  .bprm_check_security       = tuxguardian_bprm_check_security,

  .socket_create             =	tuxguardian_socket_create,
  .socket_connect            =  tuxguardian_socket_connect,


  .socket_bind               =	tuxguardian_socket_bind,
  .socket_listen             =	tuxguardian_socket_listen,
  .socket_accept             =	tuxguardian_socket_accept,
  .socket_sendmsg            =	tuxguardian_socket_sendmsg,
  .socket_recvmsg            = 	tuxguardian_socket_recvmsg,
  .socket_getsockname        =	tuxguardian_socket_getsockname,
  .socket_getpeername        =	tuxguardian_socket_getpeername,
  .socket_getsockopt         =	tuxguardian_socket_getsockopt,
  .socket_setsockopt         =	tuxguardian_socket_setsockopt,
  .socket_shutdown           =	tuxguardian_socket_shutdown,
  .unix_stream_connect       =	tuxguardian_socket_unix_stream_connect,
  .unix_may_send             =	tuxguardian_socket_unix_may_send,



};




static int __init tuxguardian_init (void)
{

  /* register ourselves with the security framework */
  if (register_security (&tuxguardian_security_ops)) {

    printk(KERN_INFO "Failure registering TuxGuardian module with the kernel\n");

    /* try registering as the primary module */
    if (mod_reg_security (MY_NAME, &tuxguardian_security_ops)) {
      printk(KERN_INFO "Failure registering TuxGuardian as the primary security module\n");
      return -EINVAL;
    }
  }

  printk (KERN_INFO "TuxGuardian initialized\n");
  return 0;
}



static void __exit tuxguardian_exit (void)
{

  /* remove ourselves from the security framework */
  if (unregister_security (&tuxguardian_security_ops)) {
    printk (KERN_INFO "Failure unregistering TuxGuardian\n");
  }
  else
    printk (KERN_INFO "\nTuxGuardian module removed\n");


}

security_initcall (tuxguardian_init);
module_exit (tuxguardian_exit);


/* MODULE_PARAM(int_param, "i") */
/* then passing value into module as */
/* insmod module int_param=x */

/* Initialize the module - show the parameters */
/* int init_module() */
/* { */
/*   if (str1 == NULL || str2 == NULL) { */
/*     printk("Next time, do insmod param str1=<something>"); */
/*     printk("str2=<something>\n"); */
/*   } else */
/*     printk("Strings:%s and %s\n", str1, str2); */

MODULE_AUTHOR("Bruno Castro da Silva");
MODULE_DESCRIPTION("TuxGuardian Security Module");
// thanks stallman, thanks linus
MODULE_LICENSE("GPL");