Print this page
Code review comments from jeffpc
7029 want per-process exploit mitigation features (secflags)
7030 want basic address space layout randomization (aslr)
7031 noexec_user_stack should be a secflag
7032 want a means to forbid mappings around NULL.
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libscf/common/highlevel.c
+++ new/usr/src/lib/libscf/common/highlevel.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 27 /*
28 28 * This file contains high level functions used by multiple utilities.
29 29 */
↓ open down ↓ |
29 lines elided |
↑ open up ↑ |
30 30
31 31 #include "libscf_impl.h"
32 32
33 33 #include <assert.h>
34 34 #include <libuutil.h>
35 35 #include <string.h>
36 36 #include <stdlib.h>
37 37 #include <sys/systeminfo.h>
38 38 #include <sys/uadmin.h>
39 39 #include <sys/utsname.h>
40 +#include <sys/secflags.h>
40 41
41 42 #ifdef __x86
42 43 #include <smbios.h>
43 44
44 45 /*
45 46 * Check whether the platform is on the fastreboot_blacklist.
46 47 * Return 1 if the platform has been blacklisted, 0 otherwise.
47 48 */
48 49 static int
49 50 scf_is_fb_blacklisted(void)
50 51 {
51 52 smbios_hdl_t *shp;
52 53 smbios_system_t sys;
53 54 smbios_info_t info;
54 55
55 56 id_t id;
56 57 int err;
57 58 int i;
58 59
59 60 scf_simple_prop_t *prop = NULL;
60 61 ssize_t numvals;
61 62 char *platform_name;
62 63
63 64 int blacklisted = 0;
64 65
65 66 /*
66 67 * If there's no SMBIOS, assume it's blacklisted.
67 68 */
68 69 if ((shp = smbios_open(NULL, SMB_VERSION, 0, &err)) == NULL)
69 70 return (1);
70 71
71 72 /*
72 73 * If we can't read system info, assume it's blacklisted.
73 74 */
74 75 if ((id = smbios_info_system(shp, &sys)) == SMB_ERR ||
75 76 smbios_info_common(shp, id, &info) == SMB_ERR) {
76 77 blacklisted = 1;
77 78 goto fb_out;
78 79 }
79 80
80 81 /*
81 82 * If we can't read the "platforms" property from property group
82 83 * BOOT_CONFIG_PG_FBBLACKLIST, assume no platforms have
83 84 * been blacklisted.
84 85 */
85 86 if ((prop = scf_simple_prop_get(NULL, FMRI_BOOT_CONFIG,
86 87 BOOT_CONFIG_PG_FBBLACKLIST, "platforms")) == NULL)
87 88 goto fb_out;
88 89
89 90 numvals = scf_simple_prop_numvalues(prop);
90 91
91 92 for (i = 0; i < numvals; i++) {
92 93 platform_name = scf_simple_prop_next_astring(prop);
93 94 if (platform_name == NULL)
94 95 break;
95 96 if (strcmp(platform_name, info.smbi_product) == 0) {
96 97 blacklisted = 1;
97 98 break;
98 99 }
99 100 }
100 101
101 102 fb_out:
102 103 smbios_close(shp);
103 104 scf_simple_prop_free(prop);
104 105
105 106 return (blacklisted);
106 107 }
107 108
108 109 /*
109 110 * Add or get a property group given an FMRI.
110 111 * Return SCF_SUCCESS on success, SCF_FAILED on failure.
111 112 */
112 113 static int
113 114 scf_fmri_pg_get_or_add(const char *fmri, const char *pgname,
114 115 const char *pgtype, uint32_t pgflags, int add)
115 116 {
116 117 scf_handle_t *handle = NULL;
117 118 scf_instance_t *inst = NULL;
118 119 int rc = SCF_FAILED;
119 120 int error;
120 121
121 122 if ((handle = scf_handle_create(SCF_VERSION)) == NULL ||
122 123 scf_handle_bind(handle) != 0 ||
123 124 (inst = scf_instance_create(handle)) == NULL ||
124 125 scf_handle_decode_fmri(handle, fmri, NULL, NULL,
125 126 inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) != SCF_SUCCESS)
126 127 goto scferror;
127 128
128 129 if (add) {
129 130 rc = scf_instance_add_pg(inst, pgname, pgtype, pgflags, NULL);
130 131 /*
131 132 * If the property group already exists, return SCF_SUCCESS.
132 133 */
133 134 if (rc != SCF_SUCCESS && scf_error() == SCF_ERROR_EXISTS)
134 135 rc = SCF_SUCCESS;
135 136 } else {
136 137 rc = scf_instance_get_pg(inst, pgname, NULL);
137 138 }
138 139
139 140 scferror:
140 141 if (rc != SCF_SUCCESS)
141 142 error = scf_error();
142 143
143 144 scf_instance_destroy(inst);
144 145 if (handle)
145 146 (void) scf_handle_unbind(handle);
146 147 scf_handle_destroy(handle);
147 148
148 149 if (rc != SCF_SUCCESS)
149 150 (void) scf_set_error(error);
150 151
151 152 return (rc);
152 153 }
153 154 #endif /* __x86 */
154 155
155 156 /*
156 157 * Get config properties from svc:/system/boot-config:default.
157 158 * It prints errors with uu_warn().
158 159 */
159 160 void
160 161 scf_get_boot_config(uint8_t *boot_config)
161 162 {
162 163 uint64_t ret = 0;
163 164
164 165 assert(boot_config);
165 166 *boot_config = 0;
166 167
167 168 {
168 169 /*
169 170 * Property vector for BOOT_CONFIG_PG_PARAMS property group.
170 171 */
171 172 scf_propvec_t ua_boot_config[] = {
172 173 { FASTREBOOT_DEFAULT, NULL, SCF_TYPE_BOOLEAN, NULL,
173 174 UA_FASTREBOOT_DEFAULT },
174 175 { FASTREBOOT_ONPANIC, NULL, SCF_TYPE_BOOLEAN, NULL,
175 176 UA_FASTREBOOT_ONPANIC },
176 177 { NULL }
177 178 };
178 179 scf_propvec_t *prop;
179 180
180 181 for (prop = ua_boot_config; prop->pv_prop != NULL; prop++)
181 182 prop->pv_ptr = &ret;
182 183 prop = NULL;
183 184 if (scf_read_propvec(FMRI_BOOT_CONFIG, BOOT_CONFIG_PG_PARAMS,
184 185 B_TRUE, ua_boot_config, &prop) != SCF_FAILED) {
185 186
186 187 #ifdef __x86
187 188 /*
188 189 * Unset both flags if the platform has been
189 190 * blacklisted.
190 191 */
191 192 if (scf_is_fb_blacklisted())
192 193 return;
193 194 #endif /* __x86 */
194 195 *boot_config = (uint8_t)ret;
195 196 return;
196 197 }
197 198 #if defined(FASTREBOOT_DEBUG)
198 199 if (prop != NULL) {
199 200 (void) uu_warn("Service %s property '%s/%s' "
200 201 "not found.\n", FMRI_BOOT_CONFIG,
201 202 BOOT_CONFIG_PG_PARAMS, prop->pv_prop);
202 203 } else {
203 204 (void) uu_warn("Unable to read service %s "
204 205 "property '%s': %s\n", FMRI_BOOT_CONFIG,
205 206 BOOT_CONFIG_PG_PARAMS, scf_strerror(scf_error()));
206 207 }
207 208 #endif /* FASTREBOOT_DEBUG */
208 209 }
209 210 }
210 211
211 212 /*
212 213 * Get or set properties in non-persistent "config_ovr" property group
213 214 * in svc:/system/boot-config:default.
214 215 * It prints errors with uu_warn().
215 216 */
216 217 /*ARGSUSED*/
217 218 static int
218 219 scf_getset_boot_config_ovr(int set, uint8_t *boot_config_ovr)
219 220 {
220 221 int rc = SCF_SUCCESS;
221 222
222 223 assert(boot_config_ovr);
223 224
224 225 #ifndef __x86
225 226 return (rc);
226 227 #else
227 228 {
228 229 /*
229 230 * Property vector for BOOT_CONFIG_PG_OVR property group.
230 231 */
231 232 scf_propvec_t ua_boot_config_ovr[] = {
232 233 { FASTREBOOT_DEFAULT, NULL, SCF_TYPE_BOOLEAN, NULL,
233 234 UA_FASTREBOOT_DEFAULT },
234 235 { FASTREBOOT_ONPANIC, NULL, SCF_TYPE_BOOLEAN, NULL,
235 236 UA_FASTREBOOT_ONPANIC },
236 237 { NULL }
237 238 };
238 239 scf_propvec_t *prop;
239 240
240 241 rc = scf_fmri_pg_get_or_add(FMRI_BOOT_CONFIG,
241 242 BOOT_CONFIG_PG_OVR, SCF_GROUP_APPLICATION,
242 243 SCF_PG_FLAG_NONPERSISTENT, set);
243 244
244 245 if (rc != SCF_SUCCESS) {
245 246 #if defined(FASTREBOOT_DEBUG)
246 247 if (set)
247 248 (void) uu_warn("Unable to add service %s "
248 249 "property group '%s'\n",
249 250 FMRI_BOOT_CONFIG, BOOT_CONFIG_PG_OVR);
250 251 #endif /* FASTREBOOT_DEBUG */
251 252 return (rc);
252 253 }
253 254
254 255 for (prop = ua_boot_config_ovr; prop->pv_prop != NULL; prop++)
255 256 prop->pv_ptr = boot_config_ovr;
256 257 prop = NULL;
257 258
258 259 if (set)
259 260 rc = scf_write_propvec(FMRI_BOOT_CONFIG,
260 261 BOOT_CONFIG_PG_OVR, ua_boot_config_ovr, &prop);
261 262 else
262 263 rc = scf_read_propvec(FMRI_BOOT_CONFIG,
263 264 BOOT_CONFIG_PG_OVR, B_FALSE, ua_boot_config_ovr,
264 265 &prop);
265 266
266 267 #if defined(FASTREBOOT_DEBUG)
267 268 if (rc != SCF_SUCCESS) {
268 269 if (prop != NULL) {
269 270 (void) uu_warn("Service %s property '%s/%s' "
270 271 "not found.\n", FMRI_BOOT_CONFIG,
271 272 BOOT_CONFIG_PG_OVR, prop->pv_prop);
272 273 } else {
273 274 (void) uu_warn("Unable to %s service %s "
274 275 "property '%s': %s\n", set ? "set" : "get",
275 276 FMRI_BOOT_CONFIG, BOOT_CONFIG_PG_OVR,
276 277 scf_strerror(scf_error()));
277 278 }
278 279 }
279 280 #endif /* FASTREBOOT_DEBUG */
280 281
281 282 if (set)
282 283 (void) smf_refresh_instance(FMRI_BOOT_CONFIG);
283 284
284 285 return (rc);
285 286
286 287 }
287 288 #endif /* __x86 */
288 289 }
289 290
290 291 /*
291 292 * Get values of properties in non-persistent "config_ovr" property group.
292 293 */
293 294 void
294 295 scf_get_boot_config_ovr(uint8_t *boot_config_ovr)
295 296 {
296 297 (void) scf_getset_boot_config_ovr(B_FALSE, boot_config_ovr);
297 298 }
298 299
299 300 /*
300 301 * Set value of "config_ovr/fastreboot_default".
301 302 */
302 303 int
303 304 scf_fastreboot_default_set_transient(boolean_t value)
304 305 {
305 306 uint8_t boot_config_ovr = 0;
306 307
307 308 if (value == B_TRUE)
308 309 boot_config_ovr = UA_FASTREBOOT_DEFAULT | UA_FASTREBOOT_ONPANIC;
309 310
310 311 return (scf_getset_boot_config_ovr(B_TRUE, &boot_config_ovr));
311 312 }
312 313
313 314 /*
314 315 * Check whether Fast Reboot is the default operating mode.
315 316 * Return 0 if
316 317 * 1. the platform is xVM
317 318 * or
318 319 * 2. svc:/system/boot-config:default service doesn't exist,
319 320 * or
320 321 * 3. property "config/fastreboot_default" doesn't exist,
321 322 * or
322 323 * 4. value of property "config/fastreboot_default" is set to "false"
323 324 * and "config_ovr/fastreboot_default" is not set to "true",
324 325 * or
325 326 * 5. the platform has been blacklisted.
326 327 * or
327 328 * 6. value of property "config_ovr/fastreboot_default" is set to "false".
328 329 * Return non-zero otherwise.
329 330 */
330 331 int
331 332 scf_is_fastboot_default(void)
332 333 {
333 334 uint8_t boot_config = 0, boot_config_ovr;
334 335 char procbuf[SYS_NMLN];
335 336
336 337 /*
337 338 * If we are on xVM, do not fast reboot by default.
338 339 */
339 340 if (sysinfo(SI_PLATFORM, procbuf, sizeof (procbuf)) == -1 ||
340 341 strcmp(procbuf, "i86xpv") == 0)
341 342 return (0);
342 343
343 344 /*
344 345 * Get property values from "config" property group
345 346 */
↓ open down ↓ |
296 lines elided |
↑ open up ↑ |
346 347 scf_get_boot_config(&boot_config);
347 348
348 349 /*
349 350 * Get property values from non-persistent "config_ovr" property group
350 351 */
351 352 boot_config_ovr = boot_config;
352 353 scf_get_boot_config_ovr(&boot_config_ovr);
353 354
354 355 return (boot_config & boot_config_ovr & UA_FASTREBOOT_DEFAULT);
355 356 }
357 +
358 +/*
359 + * Read the default security-flags from system/process-security and return a
360 + * secflagset_t suitable for psecflags(2)
361 + *
362 + * Unfortunately, this symbol must _exist_ in the native build, for the sake
363 + * of the mapfile, even though we don't ever use it, and it will never work.
364 + */
365 +struct group_desc {
366 + secflagset_t *set;
367 + char *fmri;
368 +};
369 +
370 +int
371 +scf_default_secflags(scf_handle_t *hndl, psecflags_t *flags)
372 +{
373 +#if !defined(NATIVE_BUILD)
374 + scf_property_t *prop;
375 + scf_value_t *val;
376 + const char *flagname;
377 + int flag;
378 + struct group_desc *g;
379 + struct group_desc groups[] = {
380 + {NULL, "svc:/system/process-security/"
381 + ":properties/default"},
382 + {NULL, "svc:/system/process-security/"
383 + ":properties/lower"},
384 + {NULL, "svc:/system/process-security/"
385 + ":properties/upper"},
386 + {NULL, NULL}
387 + };
388 +
389 + groups[0].set = &flags->psf_inherit;
390 + groups[1].set = &flags->psf_lower;
391 + groups[2].set = &flags->psf_upper;
392 +
393 + /* Ensure sane defaults */
394 + psecflags_default(flags);
395 +
396 + for (g = groups; g->set != NULL; g++) {
397 + for (flag = 0; (flagname = secflag_to_str(flag)) != NULL;
398 + flag++) {
399 + char *pfmri;
400 + uint8_t flagval = 0;
401 +
402 + if ((val = scf_value_create(hndl)) == NULL)
403 + return (-1);
404 +
405 + if ((prop = scf_property_create(hndl)) == NULL) {
406 + scf_value_destroy(val);
407 + return (-1);
408 + }
409 +
410 + if ((pfmri = uu_msprintf("%s/%s", g->fmri,
411 + flagname)) == NULL)
412 + uu_die("Allocation failure\n");
413 +
414 + if (scf_handle_decode_fmri(hndl, pfmri,
415 + NULL, NULL, NULL, NULL, prop, NULL) != 0)
416 + goto next;
417 +
418 + if (scf_property_get_value(prop, val) != 0)
419 + goto next;
420 +
421 + (void) scf_value_get_boolean(val, &flagval);
422 +
423 + if (flagval != 0)
424 + secflag_set(g->set, flag);
425 + else
426 + secflag_clear(g->set, flag);
427 +
428 +next:
429 + uu_free(pfmri);
430 + scf_value_destroy(val);
431 + scf_property_destroy(prop);
432 + }
433 + }
434 +
435 + if (!psecflags_validate(flags))
436 + return (-1);
437 +
438 + return (0);
439 +#else
440 + assert(0);
441 + abort();
442 +#endif /* !NATIVE_BUILD */
443 +}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX