1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/param.h>
#include <sys/atomic.h>
#include <sys/kmem.h>
#include <sys/sysmacros.h>
#include <sys/procset.h>
#include <sys/corectl.h>
#include <sys/zone.h>
#include <sys/cmn_err.h>
#include <sys/policy.h>
/*
* Core File Settings
* ------------------
*
* A process's core file path and content live in separate reference-counted
* structures. The corectl_content_t structure is fairly straightforward --
* the only subtlety is that we only really _need_ the mutex on architectures
* on which 64-bit memory operations are not atomic. The corectl_path_t
* structure is slightly trickier in that it contains a refstr_t rather than
* just a char * string. This is to allow consumers of the data in that
* structure (the core dumping sub-system for example) to safely use the
* string without holding any locks on it in light of updates.
*
* At system and zone boot, init_core() sets init(8)'s core file path and
* content to the same value as the fields core_default_path and
* core_default_content respectively (for the global zone). All subsequent
* children of init(8) reference those same settings. During boot coreadm(8)
* is invoked with the -u option to update the system settings from
* /etc/coreadm.conf. This has the effect of also changing the values in
* core_default_path and core_default_content which updates the core file
* settings for all processes in the zone. Each zone has different default
* settings; when processes enter a non-global zone, their core file path and
* content are set to the zone's default path and content.
*
* Processes that have their core file settings explicitly overridden using
* coreadm(8) no longer reference core_default_path or core_default_content
* so subsequent changes to the default will not affect them.
*/
zone_key_t core_zone_key;
static int set_proc_info(pid_t pid, const char *path, core_content_t content);
static corectl_content_t *
corectl_content_alloc(core_content_t cc)
{
corectl_content_t *ccp;
ccp = kmem_zalloc(sizeof (corectl_content_t), KM_SLEEP);
ccp->ccc_content = cc;
ccp->ccc_refcnt = 1;
return (ccp);
}
core_content_t
corectl_content_value(corectl_content_t *ccp)
{
core_content_t content;
mutex_enter(&ccp->ccc_mtx);
content = ccp->ccc_content;
mutex_exit(&ccp->ccc_mtx);
return (content);
}
static void
corectl_content_set(corectl_content_t *ccp, core_content_t content)
{
mutex_enter(&ccp->ccc_mtx);
ccp->ccc_content = content;
mutex_exit(&ccp->ccc_mtx);
}
void
corectl_content_hold(corectl_content_t *ccp)
{
atomic_inc_32(&ccp->ccc_refcnt);
}
void
corectl_content_rele(corectl_content_t *ccp)
{
if (atomic_dec_32_nv(&ccp->ccc_refcnt) == 0)
kmem_free(ccp, sizeof (corectl_content_t));
}
static corectl_path_t *
corectl_path_alloc(const char *path)
{
corectl_path_t *ccp;
ccp = kmem_zalloc(sizeof (corectl_path_t), KM_SLEEP);
ccp->ccp_path = refstr_alloc(path);
ccp->ccp_refcnt = 1;
return (ccp);
}
refstr_t *
corectl_path_value(corectl_path_t *ccp)
{
refstr_t *path;
mutex_enter(&ccp->ccp_mtx);
refstr_hold(path = ccp->ccp_path);
mutex_exit(&ccp->ccp_mtx);
return (path);
}
static void
corectl_path_set(corectl_path_t *ccp, const char *path)
{
refstr_t *npath = refstr_alloc(path);
mutex_enter(&ccp->ccp_mtx);
refstr_rele(ccp->ccp_path);
ccp->ccp_path = npath;
mutex_exit(&ccp->ccp_mtx);
}
void
corectl_path_hold(corectl_path_t *ccp)
{
atomic_inc_32(&ccp->ccp_refcnt);
}
void
corectl_path_rele(corectl_path_t *ccp)
{
if (atomic_dec_32_nv(&ccp->ccp_refcnt) == 0) {
refstr_rele(ccp->ccp_path);
kmem_free(ccp, sizeof (corectl_path_t));
}
}
/*
* Constructor routine to be called when a zone is created.
*/
/*ARGSUSED*/
static void *
core_init_zone(zoneid_t zoneid)
{
struct core_globals *cg;
cg = kmem_alloc(sizeof (*cg), KM_SLEEP);
mutex_init(&cg->core_lock, NULL, MUTEX_DEFAULT, NULL);
cg->core_file = NULL;
cg->core_options = CC_PROCESS_PATH;
cg->core_content = CC_CONTENT_DEFAULT;
cg->core_rlimit = RLIM64_INFINITY;
cg->core_default_path = corectl_path_alloc("core");
cg->core_default_content = corectl_content_alloc(CC_CONTENT_DEFAULT);
return (cg);
}
/*
* Destructor routine to be called when a zone is destroyed.
*/
/*ARGSUSED*/
static void
core_free_zone(zoneid_t zoneid, void *arg)
{
struct core_globals *cg = arg;
if (cg == NULL)
return;
if (cg->core_file != NULL)
refstr_rele(cg->core_file);
corectl_path_rele(cg->core_default_path);
corectl_content_rele(cg->core_default_content);
kmem_free(cg, sizeof (*cg));
}
/*
* Called from start_init_common(), to set init's core file path and content.
*/
void
init_core(void)
{
struct core_globals *cg;
/*
* The first time we hit this, in the global zone, we have to
* initialize the zsd key.
*/
if (INGLOBALZONE(curproc)) {
zone_key_create(&core_zone_key, core_init_zone, NULL,
core_free_zone);
}
/*
* zone_key_create will have called core_init_zone for the
* global zone, which sets up the default path and content
* variables.
*/
VERIFY((cg = zone_getspecific(core_zone_key, curproc->p_zone)) != NULL);
corectl_path_hold(cg->core_default_path);
corectl_content_hold(cg->core_default_content);
curproc->p_corefile = cg->core_default_path;
curproc->p_content = cg->core_default_content;
}
int
corectl(int subcode, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3)
{
int error = 0;
proc_t *p;
refstr_t *rp;
size_t size;
char *path;
core_content_t content = CC_CONTENT_INVALID;
struct core_globals *cg;
zone_t *zone = curproc->p_zone;
cg = zone_getspecific(core_zone_key, zone);
ASSERT(cg != NULL);
switch (subcode) {
case CC_SET_OPTIONS:
if ((error = secpolicy_coreadm(CRED())) == 0) {
if (arg1 & ~CC_OPTIONS)
error = EINVAL;
else
cg->core_options = (uint32_t)arg1;
}
break;
case CC_GET_OPTIONS:
return (cg->core_options);
case CC_GET_GLOBAL_PATH:
case CC_GET_DEFAULT_PATH:
case CC_GET_PROCESS_PATH:
if (subcode == CC_GET_GLOBAL_PATH) {
mutex_enter(&cg->core_lock);
if ((rp = cg->core_file) != NULL)
refstr_hold(rp);
mutex_exit(&cg->core_lock);
} else if (subcode == CC_GET_DEFAULT_PATH) {
rp = corectl_path_value(cg->core_default_path);
} else {
rp = NULL;
mutex_enter(&pidlock);
if ((p = prfind((pid_t)arg3)) == NULL ||
p->p_stat == SIDL) {
mutex_exit(&pidlock);
error = ESRCH;
} else {
mutex_enter(&p->p_lock);
mutex_exit(&pidlock);
mutex_enter(&p->p_crlock);
if (!hasprocperm(p->p_cred, CRED()))
error = EPERM;
else if (p->p_corefile != NULL)
rp = corectl_path_value(p->p_corefile);
mutex_exit(&p->p_crlock);
mutex_exit(&p->p_lock);
}
}
if (rp == NULL) {
if (error == 0 && suword8((void *)arg1, 0))
error = EFAULT;
} else {
error = copyoutstr(refstr_value(rp), (char *)arg1,
(size_t)arg2, NULL);
refstr_rele(rp);
}
break;
case CC_SET_GLOBAL_PATH:
case CC_SET_DEFAULT_PATH:
if ((error = secpolicy_coreadm(CRED())) != 0)
break;
/* FALLTHROUGH */
case CC_SET_PROCESS_PATH:
if ((size = MIN((size_t)arg2, MAXPATHLEN)) == 0) {
error = EINVAL;
break;
}
path = kmem_alloc(size, KM_SLEEP);
error = copyinstr((char *)arg1, path, size, NULL);
if (error == 0) {
if (subcode == CC_SET_PROCESS_PATH) {
error = set_proc_info((pid_t)arg3, path, 0);
} else if (subcode == CC_SET_DEFAULT_PATH) {
corectl_path_set(cg->core_default_path, path);
} else if (*path != '\0' && *path != '/') {
error = EINVAL;
} else {
refstr_t *nrp = refstr_alloc(path);
mutex_enter(&cg->core_lock);
rp = cg->core_file;
if (*path == '\0')
cg->core_file = NULL;
else
refstr_hold(cg->core_file = nrp);
mutex_exit(&cg->core_lock);
if (rp != NULL)
refstr_rele(rp);
refstr_rele(nrp);
}
}
kmem_free(path, size);
break;
case CC_SET_GLOBAL_CONTENT:
case CC_SET_DEFAULT_CONTENT:
if ((error = secpolicy_coreadm(CRED())) != 0)
break;
/* FALLTHROUGH */
case CC_SET_PROCESS_CONTENT:
error = copyin((void *)arg1, &content, sizeof (content));
if (error != 0)
break;
/*
* If any unknown bits are set, don't let this charade
* continue.
*/
if (content & ~CC_CONTENT_ALL) {
error = EINVAL;
break;
}
if (subcode == CC_SET_PROCESS_CONTENT) {
error = set_proc_info((pid_t)arg2, NULL, content);
} else if (subcode == CC_SET_DEFAULT_CONTENT) {
corectl_content_set(cg->core_default_content, content);
} else {
mutex_enter(&cg->core_lock);
cg->core_content = content;
mutex_exit(&cg->core_lock);
}
break;
case CC_GET_GLOBAL_CONTENT:
content = cg->core_content;
error = copyout(&content, (void *)arg1, sizeof (content));
break;
case CC_GET_DEFAULT_CONTENT:
content = corectl_content_value(cg->core_default_content);
error = copyout(&content, (void *)arg1, sizeof (content));
break;
case CC_GET_PROCESS_CONTENT:
mutex_enter(&pidlock);
if ((p = prfind((pid_t)arg2)) == NULL || p->p_stat == SIDL) {
mutex_exit(&pidlock);
error = ESRCH;
break;
}
mutex_enter(&p->p_lock);
mutex_exit(&pidlock);
mutex_enter(&p->p_crlock);
if (!hasprocperm(p->p_cred, CRED()))
error = EPERM;
else if (p->p_content == NULL)
content = CC_CONTENT_NONE;
else
content = corectl_content_value(p->p_content);
mutex_exit(&p->p_crlock);
mutex_exit(&p->p_lock);
if (error == 0)
error = copyout(&content, (void *)arg1,
sizeof (content));
break;
default:
error = EINVAL;
break;
}
if (error)
return (set_errno(error));
return (0);
}
typedef struct {
int cc_count;
corectl_path_t *cc_path;
corectl_content_t *cc_content;
} counter_t;
static int
set_one_proc_info(proc_t *p, counter_t *counterp)
{
corectl_path_t *corefile;
corectl_content_t *content;
mutex_enter(&p->p_crlock);
if (!(p->p_flag & SSYS) && hasprocperm(p->p_cred, CRED())) {
mutex_exit(&p->p_crlock);
counterp->cc_count++;
if (counterp->cc_path != NULL) {
corectl_path_hold(counterp->cc_path);
mutex_enter(&p->p_lock);
corefile = p->p_corefile;
p->p_corefile = counterp->cc_path;
mutex_exit(&p->p_lock);
if (corefile != NULL)
corectl_path_rele(corefile);
} else {
corectl_content_hold(counterp->cc_content);
mutex_enter(&p->p_lock);
content = p->p_content;
p->p_content = counterp->cc_content;
mutex_exit(&p->p_lock);
if (content != NULL)
corectl_content_rele(content);
}
} else {
mutex_exit(&p->p_crlock);
}
return (0);
}
static int
set_proc_info(pid_t pid, const char *path, core_content_t content)
{
proc_t *p;
counter_t counter;
int error = 0;
counter.cc_count = 0;
/*
* Only one of the core file path or content can be set at a time.
*/
if (path != NULL) {
counter.cc_path = corectl_path_alloc(path);
counter.cc_content = NULL;
} else {
counter.cc_path = NULL;
counter.cc_content = corectl_content_alloc(content);
}
if (pid == -1) {
procset_t set;
setprocset(&set, POP_AND, P_ALL, P_MYID, P_ALL, P_MYID);
error = dotoprocs(&set, set_one_proc_info, (char *)&counter);
if (error == 0 && counter.cc_count == 0)
error = EPERM;
} else if (pid > 0) {
mutex_enter(&pidlock);
if ((p = prfind(pid)) == NULL || p->p_stat == SIDL) {
error = ESRCH;
} else {
(void) set_one_proc_info(p, &counter);
if (counter.cc_count == 0)
error = EPERM;
}
mutex_exit(&pidlock);
} else {
int nfound = 0;
pid_t pgid;
if (pid == 0)
pgid = curproc->p_pgrp;
else
pgid = -pid;
mutex_enter(&pidlock);
for (p = pgfind(pgid); p != NULL; p = p->p_pglink) {
if (p->p_stat != SIDL) {
nfound++;
(void) set_one_proc_info(p, &counter);
}
}
mutex_exit(&pidlock);
if (nfound == 0)
error = ESRCH;
else if (counter.cc_count == 0)
error = EPERM;
}
if (path != NULL)
corectl_path_rele(counter.cc_path);
else
corectl_content_rele(counter.cc_content);
if (error)
return (set_errno(error));
return (0);
}
/*
* Give current process the default core settings for its current zone;
* used for processes entering a zone via zone_enter.
*/
void
set_core_defaults(void)
{
proc_t *p = curproc;
struct core_globals *cg;
corectl_path_t *oldpath, *newpath;
corectl_content_t *oldcontent, *newcontent;
cg = zone_getspecific(core_zone_key, p->p_zone);
/* make local copies of default values to protect against change */
newpath = cg->core_default_path;
newcontent = cg->core_default_content;
corectl_path_hold(newpath);
corectl_content_hold(newcontent);
mutex_enter(&p->p_lock);
oldpath = p->p_corefile;
p->p_corefile = newpath;
oldcontent = p->p_content;
p->p_content = newcontent;
mutex_exit(&p->p_lock);
if (oldpath != NULL)
corectl_path_rele(oldpath);
if (oldcontent != NULL)
corectl_content_rele(oldcontent);
}
|