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