4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd All Rights Reserved
6 * Contact: Kidong Kim <kd0228.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 #include <sys/types.h>
24 #include <sys/smack.h>
26 #ifndef _PRIVILEGE_CONTROL_H_
27 #define _PRIVILEGE_CONTROL_H_
29 /* Macros for converting preprocessor token to string */
30 #define STRINGIFY(x) #x
31 #define TOSTRING(x) STRINGIFY(x)
38 #define API __attribute__((visibility("default")))
42 #define PC_OPERATION_SUCCESS ((int)0)
43 #define PC_ERR_FILE_OPERATION -1
44 #define PC_ERR_MEM_OPERATION -2
45 #define PC_ERR_NOT_PERMITTED -3
46 #define PC_ERR_INVALID_PARAM -4
47 #define PC_ERR_INVALID_OPERATION -5
48 #define PC_ERR_DB_OPERATION -6
56 /* APIs - used by applications */
57 int control_privilege(void) __attribute__((deprecated));
59 int set_privilege(const char* pkg_name) __attribute__((deprecated));
62 * Function get process smack label base on pid.
63 * @param in: pid of process
64 * @param out: label of process
65 * @return PC_OPERATION_SUCCESS on success PC_ERR_* on error.
67 int get_smack_label_from_process(pid_t pid, char smack_label[SMACK_LABEL_LEN + 1]);
70 * Check if process with pid have access to object.
71 * This function check if subject have access to object via smack_have_access() function.
72 * If YES then returned access granted. In NO then function check if process with pid have
73 * CAP_MAC_OVERRIDE capability. If YES then return access granted.
74 * If NO then return access denied.
76 * @param pid of process
77 * @param label of object to access
79 * @return 0 (no access) or 1 (access) or -1 (error)
81 int smack_pid_have_access(pid_t pid,
83 const char *access_type);
86 * Set DAC and SMACK privileges for application.
87 * This function is meant to be call by the application launcher just before
88 * it launches an application. It will setup DAC and SMACK privileges based
89 * on app type and accesses.
90 * It must be called with root privileges, which will be dropped in the function.
92 * @param name package name
93 * @param type application type (currently distinguished types: "wgt" and other)
94 * @param path file system path to the binary
95 * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
97 int set_app_privilege(const char* name, const char* type, const char* path);
100 * For a UNIX socket endpoint determine the other side's app_id.
102 * @param sockfd socket file descriptor
103 * @return id of the connecting widget on success, NULL on failure.
104 * Caller is responsible for freeing the return widget id.
106 char* app_id_from_socket(int sockfd);
109 * Inform about installation of a new app.
110 * It is intended to be called during app installation.
111 * It will create an empty SMACK rules file used by other functions operating
112 * on permissions if it doesn't already exist. It is needed for tracking
113 * lifetime of an app. It must be called by privileged user, before using any
114 * other app_* function. It may be called more than once during installation.
117 * @param app_id application identifier
118 * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
120 int app_install(const char* app_id);
123 * Inform about deinstallation of an app.
124 * It will remove the SMACK rules file, enabling future installation of app
125 * with the same identifier. It is needed for tracking lifetime of an app.
126 * You should call app_revoke_permissions() before this function.
127 * It must be called by privileged user.
130 * @param app_id application identifier
131 * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
133 int app_uninstall(const char* app_id);
136 * Inform about installation of new Anti Virus application.
137 * It is intended to be called during Anti Virus installation.
138 * It will give this application SMACK rules to RWX access to all other apps
139 * installed in system.
140 * It must be called by privileged user.
142 * @param app_id application identifier
143 * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error.
145 int app_register_av(const char* app_av_id);
148 * Grant SMACK permissions based on permissions list.
149 * It is intended to be called during app installation.
150 * It will construct SMACK rules based on permissions list, grant them
151 * and store it in a file, so they will be automatically granted on
153 * It must be called by privileged user.
154 * THIS FUNCTION IS NOW DEPRECATED. app_enable_permissions() SHOULD BE USED INSTEAD.
157 * @param app_id application identifier
158 * @param perm_list array of permission names, last element must be NULL
159 * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
161 int app_add_permissions(const char* app_id, const char** perm_list) __attribute__((deprecated));
164 * Grant temporary SMACK permissions based on permissions list.
165 * It will construct SMACK rules based on permissions list, grant them,
166 * but not store it anywhere, so they won't be granted again on system boot.
167 * It must be called by privileged user.
168 * THIS FUNCTION IS NOW DEPRECATED. app_enable_permissions() SHOULD BE USED INSTEAD.
171 * @param app_id application identifier
172 * @param perm_list array of permission names, last element must be NULL
173 * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
175 int app_add_volatile_permissions(const char* app_id, const char** perm_list) __attribute__((deprecated));
178 * Grant SMACK permissions based on permissions list.
179 * It is intended to be called during app installation.
180 * It will construct SMACK rules based on permissions list, grant them
181 * and store it in a file, so they will be automatically granted on
182 * system boot, when persistent mode is enabled.
183 * It must be called by privileged user.
186 * @param app_id application identifier
187 * @param app_type application type
188 * @param perm_list array of permission names, last element must be NULL
189 * @param persistent boolean for choosing between persistent and temporary rules
190 * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
192 int app_enable_permissions(const char* app_id, app_type_t app_type, const char** perm_list, bool persistent);
195 * Remove previously granted SMACK permissions based on permissions list.
196 * It will remove given permissions from an app, leaving other granted
197 * permissions untouched. Results will be persistent.
198 * It must be called by privileged user.
201 * @param app_id application identifier
202 * @param app_type application type
203 * @param perm_list array of permission names, last element must be NULL
204 * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
206 int app_disable_permissions(const char* app_id, app_type_t app_type, const char** perm_list);
209 * Revoke SMACK permissions from an application.
210 * This function should be called during app deinstallation.
211 * It will revoke all SMACK rules previously granted by app_add_permissions().
212 * It will also remove a rules file from disk.
213 * It must be called by privileged user.
215 * @param app_id application identifier
216 * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
218 int app_revoke_permissions(const char* app_id);
221 * Reset SMACK permissions for an application by revoking all previously
222 * granted rules and enabling them again from a rules file from disk.
223 * It must be called by privileged user.
225 * @param app_id application identifier
226 * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
228 int app_reset_permissions(const char* app_id);
231 * Recursively set SMACK access labels for an application directory
232 * and execute labels for executable files.
233 * This function should be called once during app installation.
234 * Results will be persistent on the file system.
235 * It must be called by privileged user.
237 * @param app_label label name
238 * @param path directory path
239 * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
241 int app_label_dir(const char* app_label, const char* path);
244 * Recursively set SMACK access and transmute labels for an application
245 * directory and adds SMACK rule for application.
246 * This function should be called once during app installation.
247 * Results will be persistent on the file system.
248 * It must be called by privileged user.
249 * Labels app_label and shared_label should not be equal.
251 * @param app_label label name, used as subject for SMACK rule
252 * @param shared_label, used as object for SMACK rule
253 * @param path directory path
254 * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
256 int app_label_shared_dir(const char* app_label, const char* shared_label,
262 * Add SMACK rx rules for application identifiers to shared_label.
263 * This function should be called during app installation.
264 * It must be called by privileged user.
266 * @param shared_label label of the shared resource
267 * @param app_list list of application SMACK identifiers
268 * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
270 int add_shared_dir_readers(const char* shared_label, const char** app_list);
273 * Make two applications "friends", by giving them both full permissions on
275 * Results will be persistent on the file system. Must be called after
276 * app_add_permissions() has been called for each application.
277 * It must be called by privileged user.
279 * @param app_id1 first application identifier
280 * @param app_id2 second application identifier
281 * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
283 int app_add_friend(const char* app_id1, const char* app_id2);
286 * Modify SMACK rules to give access from (subject)customer_label to (object)
288 * Note: This function will do nothing if subject has already rwxat access to
289 * object. You can revoke this modyfication by calling app_rovoke_access.
291 * @param subject - label of client application
292 * @param object - label of provider application
293 * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
295 int app_give_access(const char* subject, const char* object, const char* permission) __attribute__ ((deprecated));
298 * Revoke access granted by app_give_access. This function will not remove
299 * accesses that were granted before app_give_access call.
301 * @param subject - label of client application
302 * @param object - label of provider application
303 * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
305 int app_revoke_access(const char* subject, const char* object) __attribute__ ((deprecated));
308 * Adds new api feature by installing new *.smack file.
309 * It must be called by privileged user.
311 * @param app_type application type
312 * @param api_feature_name name of newly added feature
313 * @param smack_rule_set set of rules required by the feature - NULL terminated
314 * list of NULL terminated rules.
315 * @param list_of_db_gids list of gids required to access databases controlled
317 * @return PC_OPERATION_SUCCESS on success, PC_ERR_* on error
319 int add_api_feature(app_type_t app_type,
320 const char* api_feature_name,
321 const char** set_smack_rule_set,
322 const gid_t* list_of_db_gids,
327 #endif // __cplusplus
329 #endif // _PRIVILEGE_CONTROL_H_