d49180183c16609d28ea36a35e810cbe953892ea
[AGL/documentation.git] / docs / 1_Architecture_Guides / 1.2_Security_Blueprint / 5_Platform / 1.2.5.5_Application_framework.md
1 ---
2 edit_link: ''
3 title: Application Framework
4 origin_url: >-
5   https://raw.githubusercontent.com/automotive-grade-linux/docs-sources/master/docs/security-blueprint/part-5/5-AppFw.md
6 ---
7
8 <!-- WARNING: This file is generated by fetch_docs.js using /home/boron/Documents/AGL/docs-webtemplate/site/_data/tocs/architecture/master/security_blueprint-security-blueprint-book.yml -->
9
10 # Application framework/model (**AppFw**)
11
12 The AGL application framework consists of several inter-working parts:
13
14 - **SMACK**: The kernel level **LSM** (**L**inux **S**ecurity **M**odule) that performs extended access control of the system.
15 - **Cynara**: the native gatekeeper daemon used for policy handling, updating to the database and policy checking.
16 - Security Manager: a master service, through which all security events are intended to take place.
17 - Several native application framework utilities: `afm-main-binding`, `afm-user-daemon`, `afm-system-daemon`.
18
19 The application framework manages:
20
21 - The applications and services management: Installing, Uninstalling, Listing, ...
22 - The life cycle of applications: Start -> (Pause, Resume) -> Stop.
23 - Events and signals propagation.
24 - Privileges granting and checking.
25 - API for interaction with applications.
26
27 <!-- section-note -->
28
29 - The **security model** refers to the security model used to ensure security
30   and to the tools that are provided for implementing that model. It's an
31   implementation detail that should not impact the layers above the application
32   framework.
33
34 - The **security model** refers to how **DAC** (**D**iscretionary **A**ccess **C**ontrol),
35   **MAC** (Mandatory Access Control) and `Capabilities` are used by the system to
36   ensure security and privacy. It also includes features of reporting using
37   audit features and by managing logs and alerts.
38
39 <!-- end-section-note -->
40
41 The **AppFw** uses the security model to ensure the security and the privacy of
42 the applications that it manages. It must be compliant with the underlying
43 security model. But it should hide it to the applications.
44
45 <!-- section-config -->
46
47 Domain                 | Object         | Recommendations
48 ---------------------- | -------------- | --------------------------------
49 Platform-AGLFw-AppFw-1 | Security model | Use the AppFw as Security model.
50
51 <!-- end-section-config -->
52
53 See [AGL AppFw Privileges Management](http://docs.automotivelinux.org/docs/devguides/en/dev/reference/iotbzh2016/appfw/03-AGL-AppFW-Privileges-Management.pdf) and [AGL - Application Framework Documentation](http://iot.bzh/download/public/2017/SDK/AppFw-Documentation-v3.1.pdf) for more
54 information.
55
56 <!-- pagebreak -->
57
58 The Security Manager communicates policy information to **Cynara**,
59 which retains information in its own database in the format of a text
60 file with comma-separated values (CSV). There are provisions to retain
61 a copy of the CSV text file when the file is being updated.
62
63 Runtime checking occurs through **Cynara**. Each application that is
64 added to the framework has its own instantiation of a SMACK context
65 and D-bus bindings. The afb_daemon and Binder form a web-service that
66 is communicated to through http or a websocket from the application-proper.
67 This http or websocket interface uses a standard unique web token for API communication.
68
69 ![Application Framework Flow](App-flow.png)
70
71 ## Cynara
72
73 There's a need for another mechanism responsible for checking applicative
74 permissions: Currently in AGL, this task depends on a policy-checker service
75 (**Cynara**).
76
77 - Stores complex policies in databases.
78 - "Soft" security (access is checked by the framework).
79
80 Cynara interact with **D-Bus** in order to deliver this information.
81
82 Cynara consists of several parts:
83
84 - Cynara: a daemon for controlling policies and responding to access control requests.
85 - Database: a spot to hold policies.
86 - Libraries: several static and dynamic libraries for communicating with Cynara.
87
88 The daemon communicates to the libraries over Unix domain sockets.
89 The database storage format is a series of CSV-like files with an index file.
90
91 There are several ways that an attacker can manipulate policies of the Cynara system:
92
93 - Disable Cynara by killing the process.
94 - Tamper with the Cynara binary on-disk or in-memory.
95 - Corrupt the database controlled by Cynara.
96 - Tamper with the database controlled by Cynara.
97 - Highjack the communication between Cynara and the database.
98
99 The text-based database is the weakest part of the system and although there are some
100 consistency mechanisms in place (i.e. the backup guard), these mechanisms are weak at best
101 and can be countered by an attacker very easily.
102
103 <!-- section-config -->
104
105 Domain                  | Object      | Recommendations
106 ----------------------- | ----------- | -------------------------------------
107 Platform-AGLFw-Cynara-1 | Permissions | Use Cynara as policy-checker service.
108
109 <!-- end-section-config -->
110
111 ### Policies
112
113 - Policy rules:
114
115   - Are simple - for pair [application context, privilege] there is straight
116     answer (single Policy Type): [ALLOW / DENY / ...].
117   - No code is executed (no script).
118   - Can be easily cached and managed.
119
120 - Application context (describes id of the user and the application credentials)
121   It is build of:
122
123   - UID of the user that runs the application.
124   - **SMACK** label of application.
125
126 ## Holding policies
127
128 Policies are kept in buckets. Buckets are set of policies which have additional
129 a property of default answer, the default answer is yielded if no policy matches
130 searched key. Buckets have names which might be used in policies (for directions).
131
132 ## Attack Vectors
133
134 The following attack vectors are not completely independent. While attackers may
135 have varying levels of access to an AGL system, experience has shown that a typical
136 attack can start with direct access to a system, find the vulnerabilities,
137 then proceed to automate the attack such that it can be invoked from less accessible
138 standpoint (e.g. remotely). Therefore, it is important to assess all threat levels,
139 and protect the system appropriately understanding that direct access attacks
140 are the door-way into remote attacks.
141
142 ### Remote Attacks
143
144 The local web server interface used for applications is the first point of attack,
145 as web service APIs are well understood and easily intercepted. The local web server
146 could potentially be exploited by redirecting web requests through the local service
147 and exploiting the APIs. While there is the use of a security token on the web
148 service API, this is weak textual matching at best. This will not be difficult to spoof.
149 It is well known that [API Keys do not provide any real security](http://nordicapis.com/why-api-keys-are-not-enough/).
150
151 It is likely that the architectural inclusion of an http / web-service interface
152 provided the most flexibility for applications to be written natively or in HTML5.
153 However, this flexibility may trade-off with security concerns. For example,
154 if a native application were linked directly to the underlying framework services,
155 there would be fewer concerns over remote attacks coming through the web-service interface.
156
157 Leaving the interface as designed, mitigations to attacks could include further
158 securing the interface layer with cryptographic protocols:
159 e.g. encrypted information passing, key exchange (e.g. Elliptic-Curve Diffie-Hellman).
160
161 ### User-level Native Attacks
162
163 - Modifying the CSV data-base
164 - Modifying the SQLite DB
165 - Tampering with the user-level binaries
166 - Tampering with the user daemons
167 - Spoofing the D-bus Interface
168 - Adding executables/libraries
169
170 With direct access to the device, there are many security concerns on the native level.
171 For example, as **Cynara** uses a text file data-base with comma-separated values (CSV),
172 an attacker could simply modify the data-base to escalate privileges of an application.
173 Once a single application has all the privileges possible on the system, exploits can
174 come through in this manner. Similarly the SQLite database used by the Security Manager
175 is not much different than a simple text file. There are many tools available to add,
176 remove, modify entries in an SQLite data-base.
177
178 On the next level, a common point of attack is to modify binaries or daemons for exploiting
179 functionality. There are many Linux tools available to aid in this regard,
180 including: [IDA Pro](https://www.hex-rays.com/products/ida/index.shtml),
181 and [radare2](https://rada.re/r/). With the ability to modify binaries,
182 an attacker can do any number of activities including: removing calls to security checks,
183 redirecting control to bypass verification functionality, ignoring security policy handling,
184 escalating privileges, etc.
185
186 Additionally, another attack vector would be to spoof the D-bus interface. D-bus is a
187 message passing system built upon Inter-Process Communication (IPC), where structured
188 messages are passed based upon a protocol. The interface is generic and well documented.
189 Therefore, modifying or adding binaries/libraries to spoof this interface is a relatively
190 straight-forward process. Once the interface has been spoofed, the attacker can issue any
191 number of commands that lead into control of low-level functionality.
192
193 Protecting a system from native attacks requires a methodical approach. First, the system
194 should reject processes that are not sanctioned to run. Signature-level verification at
195 installation time will help in this regard, but run-time integrity verification is much better.
196 Signatures need to originate from authorized parties, which is discussed further
197 in a later section on the Application Store.
198
199 On the next level, executables should not be allowed to do things where they have not been
200 granted permission. DAC and SMACK policies can help in this regard. On the other hand,
201 there remain concerns with memory accesses, system calls, and other process activity
202 that may go undetected. For this reason, a secure environment which monitors all activity
203 can give indication of all unauthorized activity on the system.
204
205 Finally, it is very difficult to catch attacks of direct tampering in a system.
206 These types of attacks require a defense-in-depth approach, where complementary software
207 protection and hardening techniques are needed. Tamper-resistance and anti-reverse-engineering
208 technologies include program transformations/obfuscation, integrity verification,
209 and white-box cryptography. If applied in a mutually-dependent fashion and considering
210 performance/security tradeoffs, the approach can provide an effective barrier
211 to direct attacks to the system. Furthermore, the use of threat monitoring provides a
212 valuable telemetry/analytics capability and the ability to react and renew a system under attack.
213
214 ### Root-level Native Attacks
215
216 - Tampering the system daemon
217 - Tampering Cynara
218 - Tampering the security manager
219 - Disabling SMACK
220 - Tampering the kernel
221
222 Once root-level access (i.e. su) has been achieved on the device, there are many ways
223 to compromise the system. The system daemon, **Cynara**, and the security manager are
224 vulnerable to tampering attacks. For example, an executable can be modified in memory
225 to jam a branch, jump to an address, or disregard a check. This can be as simple as replacing
226 a branch instruction with a NOP, changing a memory value, or using a debugger (e.g. gdb, IDA)
227 to change an instruction. Tampering these executables would mean that policies can be
228 ignored and verification checks can be bypassed.
229
230 Without going so far as to tamper an executable, the **SMACK** system is also vulnerable to attack.
231 For example, if the kernel is stopped and restarted with the *security=none* flag,
232 then SMACK is not enabled. Furthermore, `systemd` starts the loading of **SMACK** rules during
233 start-up. If this start-up process is interfered with, then **SMACK** will not run.
234 Alternatively, new policies can be added with `smackload` allowing unforseen privileges
235 to alternative applications/executables.
236
237 Another intrusion on the kernel level is to rebuild the kernel (as it is open-source)
238 and replace it with a copy that has **SMACK** disabled, or even just the **SMACK** filesystem
239 (`smackfs`) disabled. Without the extended label attributes, the **SMACK** system is disabled.
240
241 Root-level access to the device has ultimate power, where the entire system can be compromised.
242 More so, a system with this level access allows an attacker to craft a simpler *point-attack*
243 which can operate on a level requiring fewer privileges (e.g. remote access, user-level access).
244
245 ## Vulnerable Resources
246
247 ### Resource: `afm-user-daemon`
248
249 The `afm-user-daemon` is in charge of handling applications on behalf of a user. Its main tasks are:
250
251 - Enumerate applications that the end user can run and keep this list available on demand.
252 - Start applications on behalf of the end user, set user running environment, set user security context.
253 - List current runnable or running applications.
254 - Stop (aka pause), continue (aka resume), terminate a running instance of a given application.
255 - Transfer requests for installation/uninstallation of applications to the corresponding system daemon afm-system-daemon.
256
257 The `afm-user-daemon` launches applications. It builds a secure environment for the application
258 before starting it within that environment. Different kinds of applications can be launched,
259 based on a configuration file that describes how to launch an application of a given kind within
260 a given launching mode: local or remote. Launching an application locally means that
261 the application and its binder are launched together. Launching an application remotely
262 translates in only launching the application binder.
263
264 The UI by itself has to be activated remotely by a request (i.e. HTML5 homescreen in a browser).
265 Once launched, running instances of the application receive a `runid` that identifies them.
266 `afm-user-daemon` manages the list of applications that it has launched.
267 When owning the right permissions, a client can get the list of running instances and details
268 about a specific running instance. It can also terminate, stop or continue a given application.
269 If the client owns the right permissions, `afm-user-daemon` delegates the task of
270 installing and uninstalling applications to `afm-system-daemon`.
271
272 `afm-user-daemon` is launched as a `systemd` service attached to a user session.
273 Normally, the service file is located at /usr/lib/systemd/user/afm-user-daemon.service.
274
275 Attacker goals:
276
277 - Disable `afm-user-daemon`.
278 - Tamper with the `afm-user-daemon` configuration.
279   - /usr/lib/systemd/user/afm-user-daemon.service.
280   - Application(widget) config.xml file.
281   - /etc/afm/afm-launch.conf (launcher configuration).
282
283 - Escalate user privileges to gain more access with `afm-user-daemon`.
284 - Install malicious application (widget).
285 - Tamper with `afm-user-daemon` on disk or in memory.
286
287 ### Resource: `afm-system-daemon`
288
289 The `afm-system-daemon` is in charge of installing applications on the AGL system. Its main tasks are:
290
291 - Install applications and setup security framework for newly installed applications.
292 - Uninstall applications.
293
294 `afm-system-daemon` is launched as a `systemd` service attached to system. Normally,
295 the service file is located at /lib/systemd/system/afm-systemdaemon.service.
296
297 Attacker goals:
298
299 - Disable `afm-system-daemon`.
300 - Tamper with the `afm-system-daemon` configuration.
301 - Tamper `afm-system-daemon` on disk or in memory.
302
303 ### Resource `afb-daemon`
304
305 `afb-binder` is in charge of serving resources and features through an HTTP interface.
306 `afb-daemon` is in charge of binding one instance of an application to the AGL framework
307 and AGL system. The application and its companion binder run in a secured and isolated
308 environment set for them. Applications are intended to access to AGL system through the binder.
309 `afb-daemon` binders serve files through HTTP protocol and offers developers the capability
310 to expose application API methods through HTTP or WebSocket protocol.
311
312 Binder bindings are used to add APIs to `afb-daemon`. The user can write a binding for `afb-daemon`.
313 The binder `afb-daemon` serves multiple purposes:
314
315 1. It acts as a gateway for the application to access the system.
316 2. It acts as an HTTP server for serving files to HTML5 applications.
317 3. It allows HTML5 applications to have native extensions subject to security enforcement for accessing hardware resources or for speeding up parts of algorithm.
318
319 Attacker goals:
320
321 - Break from isolation.
322 - Disable `afb-daemon`.
323 - Tamper `afb-demon` on disk or in memory.
324 - Tamper **capabilities** by creating/installing custom bindings for `afb-daemon`.