Fixed: Security Issue by Command Injection in DLT System.
[profile/ivi/dlt-daemon.git] / src / system / dlt-system-shell.c
1 /**
2  * @licence app begin@
3  * Copyright (C) 2012  BMW AG
4  *
5  * This file is part of GENIVI Project Dlt - Diagnostic Log and Trace console apps.
6  *
7  * Contributions are licensed to the GENIVI Alliance under one or more
8  * Contribution License Agreements.
9  *
10  * \copyright
11  * This Source Code Form is subject to the terms of the
12  * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed with
13  * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
14  *
15  *
16  * \author Lassi Marttala <lassi.lm.marttala@partner.bmw.de> BMW 2012
17  *
18  * \file dlt-system-logfile.c
19  * For further information see http://www.genivi.org/.
20  * @licence end@
21  */
22
23 /*******************************************************************************
24 **                                                                            **
25 **  SRC-MODULE: dlt-system-shell.c                                                  **
26 **                                                                            **
27 **  TARGET    : linux                                                         **
28 **                                                                            **
29 **  PROJECT   : DLT                                                           **
30 **                                                                            **
31 **  AUTHOR    : Lassi Marttala <lassi.lm.marttala@partner.bmw.de>             **
32 **              Alexander Wenzel Alexander.AW.Wenzel@bmw.de                   **
33 **                                                                            **
34 **  PURPOSE   :                                                               **
35 **                                                                            **
36 **  REMARKS   :                                                               **
37 **                                                                            **
38 **  PLATFORM DEPENDANT [yes/no]: yes                                          **
39 **                                                                            **
40 **  TO BE CHANGED BY USER [yes/no]: no                                        **
41 **                                                                            **
42 *******************************************************************************/
43
44 /*******************************************************************************
45 **                      Author Identity                                       **
46 ********************************************************************************
47 **                                                                            **
48 ** Initials     Name                       Company                            **
49 ** --------     -------------------------  ---------------------------------- **
50 **  lm          Lassi Marttala             BMW                                **
51 *******************************************************************************/
52 #include "dlt.h"
53 #include "dlt-system.h"
54
55 #include <string.h>
56 #include <stdlib.h>
57
58 DLT_IMPORT_CONTEXT(dltsystem)
59 DLT_DECLARE_CONTEXT(shellContext)
60
61 int dlt_shell_injection_callback(uint32_t service_id, void *data, uint32_t length)
62 {
63         DLT_LOG(shellContext,DLT_LOG_DEBUG,
64                         DLT_STRING("dlt-system-shell, injection callback"));
65         char text[1024];
66     int syserr = 0;
67
68         strncpy(text,data,length);
69
70         DLT_LOG(shellContext,DLT_LOG_DEBUG,
71                         DLT_STRING("dlt-system-shell, injection injection id:"),
72                         DLT_UINT32(service_id));
73         DLT_LOG(shellContext,DLT_LOG_DEBUG,
74                         DLT_STRING("dlt-system-shell, injection data:"),
75                         DLT_STRING(text));
76
77         switch(service_id)
78         {
79                 case 0x1001:
80                         if((syserr = system(text)) != 0)
81                         {
82                                 DLT_LOG(shellContext,DLT_LOG_ERROR,
83                                                 DLT_STRING("dlt-system-shell, abnormal exit status."),
84                                                 DLT_STRING(text),
85                                                 DLT_INT(syserr));
86                         }
87                         else
88                         {
89                                 DLT_LOG(shellContext,DLT_LOG_INFO,
90                                                 DLT_STRING("Shell command executed:"),
91                                                 DLT_STRING(text));                              
92                         }
93                         break;
94                 default:
95                         DLT_LOG(shellContext,DLT_LOG_ERROR,
96                                         DLT_STRING("dlt-system-shell, unknown command received."),
97                                         DLT_UINT32(service_id),
98                                         DLT_STRING(text));
99                         break;
100         }
101     return 0;
102 }
103
104 void init_shell()
105 {
106         DLT_LOG(dltsystem,DLT_LOG_DEBUG,
107                         DLT_STRING("dlt-system-shell, register callback"));
108         DLT_REGISTER_CONTEXT(shellContext,"CMD","Execute Shell commands");
109         DLT_REGISTER_INJECTION_CALLBACK(shellContext, 0x1001, dlt_shell_injection_callback);
110 }