-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
procstat: Add a 'compartments' command to list c18n compartments #2276
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
.\" Copyright (c) 2024 Capabilities Limited | ||
.\" Copyright (c) 2011 Sergey Kandaurov <[email protected]> | ||
.\" All rights reserved. | ||
.\" | ||
.\" This software was developed by SRI International, the University of | ||
.\" Cambridge Computer Laboratory (Department of Computer Science and | ||
.\" Technology), and Capabilities Limited under Defense Advanced Research | ||
.\" Projects Agency (DARPA) Contract No. FA8750-24-C-B047 ("DEC"). | ||
.\" | ||
.\" Redistribution and use in source and binary forms, with or without | ||
.\" modification, are permitted provided that the following conditions | ||
.\" are met: | ||
|
@@ -22,7 +28,7 @@ | |
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
.\" SUCH DAMAGE. | ||
.\" | ||
.Dd April 3, 2022 | ||
.Dd December 5, 2024 | ||
.Dt LIBPROCSTAT 3 | ||
.Os | ||
.Sh NAME | ||
|
@@ -47,6 +53,8 @@ | |
.Nm procstat_getargv , | ||
.Nm procstat_getauxv , | ||
.Nm procstat_getenvv , | ||
.Nm procstat_getc18n , | ||
.Nm procstat_getcompartments , | ||
.Nm procstat_getfiles , | ||
.Nm procstat_getgroups , | ||
.Nm procstat_getkstack , | ||
|
@@ -67,6 +75,7 @@ | |
.In sys/param.h | ||
.In sys/queue.h | ||
.In sys/socket.h | ||
.In cheri/c18n.h | ||
.In libprocstat.h | ||
.Ft void | ||
.Fn procstat_close "struct procstat *procstat" | ||
|
@@ -170,6 +179,19 @@ | |
.Fa "struct kinfo_proc *kp" | ||
.Fa "unsigned int *count" | ||
.Fc | ||
.Ft "int" | ||
.Fo procstat_getc18n | ||
.Fa "struct procstat *procstat" | ||
.Fa "struct kinfo_proc *kp" | ||
.Fa "struct rtld_c18n_stats *stats" | ||
.Fc | ||
.Ft "int" | ||
.Fo procstat_getcompartments | ||
.Fa "struct procstat *procstat" | ||
.Fa "struct kinfo_proc *kp" | ||
.Fa "struct cheri_c18n_compart *comparts" | ||
.Fa "u_int *ncompartsp" | ||
.Fc | ||
.Ft "char **" | ||
.Fo procstat_getenvv | ||
.Fa "struct procstat *procstat" | ||
|
@@ -570,6 +592,28 @@ argument indicates an actual error message in case of failure. | |
.It Li PS_FST_TYPE_SHM | ||
.Nm procstat_get_shm_info | ||
.El | ||
.Pp | ||
The | ||
.Fn procstat_getc18n | ||
function retrieves | ||
compartmentalization (\c | ||
.Xr c18n ) | ||
statistics for a target process, including its number of intra-process | ||
compartments, instantiated trampolines, and other values. | ||
The | ||
.Fn procstat_getcompartments | ||
function retrieves a compartment list for target process. | ||
The | ||
.Fa comparts | ||
argument is a pointer to a caller-allocated array of | ||
.Ft struct cheri_c18n_compart | ||
entries of size | ||
.Fa *ncompartsp | ||
passed by reference. | ||
On return, the compartment list is terminated with a compartment ID of | ||
.Dv CHERI_C18N_COMPART_LAST . | ||
If a terminating entry is not found in the returned array, then there was | ||
insufficient space, and the caller should allocate a larger array and retry. | ||
.Sh SEE ALSO | ||
.Xr fstat 1 , | ||
.Xr fuser 1 , | ||
|
@@ -583,6 +627,7 @@ argument indicates an actual error message in case of failure. | |
.Xr sysctl 3 , | ||
.Xr pts 4 , | ||
.Xr core 5 , | ||
.Xr c18n 7 , | ||
.Xr vnode 9 | ||
.Sh HISTORY | ||
The | ||
|
@@ -595,6 +640,9 @@ The | |
.Nm libprocstat | ||
library was written by | ||
.An Stanislav Sedov Aq Mt [email protected] . | ||
.Xr c18n 3 -related | ||
monitoring APIs were added by | ||
.An Robert N. M. Watson Aq Mt [email protected] . | ||
.Pp | ||
This manual page was written by | ||
.An Sergey Kandaurov Aq Mt [email protected] . |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
/*- | ||
* SPDX-License-Identifier: BSD-4-Clause | ||
* | ||
* Copyright (c) 2024 Capabilities Limited | ||
* Copyright (c) 2017 Dell EMC | ||
* Copyright (c) 2009 Stanislav Sedov <[email protected]> | ||
* Copyright (c) 1988, 1993 | ||
* The Regents of the University of California. All rights reserved. | ||
* | ||
* This software was developed by SRI International, the University of | ||
* Cambridge Computer Laboratory (Department of Computer Science and | ||
* Technology), and Capabilities Limited under Defense Advanced Research | ||
* Projects Agency (DARPA) Contract No. FA8750-24-C-B047 ("DEC"). | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
|
@@ -384,7 +390,7 @@ procstat_getc18n(struct procstat *procstat, struct kinfo_proc *kp, | |
|
||
name[0] = CTL_KERN; | ||
name[1] = KERN_PROC; | ||
name[2] = KERN_PROC_C18N; | ||
name[2] = KERN_PROC_C18N_STATS; | ||
name[3] = kp->ki_pid; | ||
if (sysctl(name, nitems(name), buf, &len, NULL, 0) != 0) { | ||
if (errno != ESRCH && errno != EPERM && errno != ENOEXEC) | ||
|
@@ -399,6 +405,84 @@ procstat_getc18n(struct procstat *procstat, struct kinfo_proc *kp, | |
return (-1); | ||
} | ||
|
||
int | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not require the caller to allocate an array and hope for the best. :) It should return an allocated array of objects that the caller can free. You can then call the sysctl twice, once to get the size estimate and a second time to populate it. This requires fixing the sysctl to add the optimized path for querying the size I mentioned. |
||
procstat_getcompartments(struct procstat *procstat, struct kinfo_proc *kp, | ||
struct cheri_c18n_compart **comparts, size_t *ncompartsp) | ||
{ | ||
int name[4]; | ||
char *inbuf; | ||
size_t n, i, cur, size; | ||
struct cheri_c18n_compart *outbuf, *cp; | ||
|
||
if (comparts == NULL || ncompartsp == NULL) | ||
goto out; | ||
|
||
switch (procstat->type) { | ||
case PROCSTAT_KVM: | ||
warnx("kvm method is not supported"); | ||
goto out; | ||
|
||
case PROCSTAT_SYSCTL: | ||
break; | ||
|
||
case PROCSTAT_CORE: | ||
warnx("core method is not supported"); | ||
goto out; | ||
|
||
default: | ||
warnx("unknown access method: %d", procstat->type); | ||
goto out; | ||
} | ||
|
||
name[0] = CTL_KERN; | ||
name[1] = KERN_PROC; | ||
name[2] = KERN_PROC_C18N_COMPARTS; | ||
name[3] = kp->ki_pid; | ||
|
||
/* Estimate the size of the input buffer. */ | ||
if (sysctl(name, nitems(name), NULL, &size, NULL, 0) != 0) { | ||
if (errno != ESRCH && errno != EPERM && errno != ENOEXEC) | ||
warn("sysctl(kern.proc.c18n_compartments)"); | ||
goto out; | ||
} | ||
|
||
/* Receive the input buffer and count the number of items. */ | ||
inbuf = malloc(size); | ||
if (inbuf == NULL) | ||
goto out; | ||
if (sysctl(name, nitems(name), inbuf, &size, NULL, 0) != 0) { | ||
if (errno != ESRCH && errno != EPERM && errno != ENOEXEC) | ||
warn("sysctl(kern.proc.c18n_compartments)"); | ||
goto out_free; | ||
} | ||
for (n = 0, cur = 0; cur < size; ++n) { | ||
cp = (struct cheri_c18n_compart *)(void *)&inbuf[cur]; | ||
cur += cp->ccc_structsize; | ||
} | ||
|
||
/* Unpack elements of the input buffer into the output buffer. */ | ||
outbuf = malloc(n * sizeof(*outbuf)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My one suggestion here would be to use calloc() so that the extra padding between structures is zeroed. However, that can be fixed up as a later commit after merging. |
||
if (outbuf == NULL) | ||
goto out_free; | ||
for (i = 0, cur = 0; i < n; ++i) { | ||
cp = (struct cheri_c18n_compart *)(void *)&inbuf[cur]; | ||
cur += cp->ccc_structsize; | ||
memcpy(&outbuf[i], cp, cp->ccc_structsize); | ||
} | ||
|
||
*comparts = outbuf; | ||
*ncompartsp = n; | ||
|
||
free(inbuf); | ||
return (0); | ||
|
||
out_free: | ||
free(inbuf); | ||
out: | ||
*ncompartsp = 0; | ||
return (-1); | ||
} | ||
|
||
struct filestat_list * | ||
procstat_getfiles(struct procstat *procstat, struct kinfo_proc *kp, int mmapped) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documenting this is good, but it should be a separate commit.