Debian Codegraph @

docs/capng_change_id.3

raw
1 .TH "CAPNG_CHANGE_ID" "3" "Mar 2026" "Red Hat" "Libcap-ng API"
2 .SH NAME
3 capng_change_id \- change the credentials retaining capabilities
4 .SH "SYNOPSIS"
5 .B #include <cap-ng.h>
6 .sp
7 int capng_change_id(int uid, int gid, capng_flags_t flag);
8
9 .SH "DESCRIPTION"
10
11 This function will change uid and gid to the ones given while retaining the capabilities previously specified in capng_update. It is also possible to specify -1 for either the uid or gid in which case the function will not change the uid or gid and leave it "as is". This is useful if you just want the flag options to be applied (assuming the option doesn't require more privileges that you currently have).
12
13 It is not necessary and perhaps better if capng_apply has not been called prior to this function so that all necessary privileges are still intact. The caller may be required to have CAP_SETPCAP capability still active before calling this function or capabilities cannot be changed. Do not specify CAP_SETPCAP in the capability sets unless you intended to have it after the uid change. The library will silently add and remove it if it can. But if its specified, it will leave it alone.
14
15 This function also takes a flag parameter that helps to tailor the exact actions performed by the function to secure the environment. The option may be or'ed together. The legal values are:
16
17 .RS
18 .TP
19 .B CAPNG_NO_FLAG
20 Simply change uid and retain specified capabilities and that's all.
21 .TP
22 .B CAPNG_DROP_SUPP_GRP
23 After changing the primary group ID, remove all supplementary groups. If no new primary group ID is supplied (gid == -1), this flag has no effect.
24 .TP
25 .B CAPNG_INIT_SUPP_GRP
26 After changing id, initialize any supplemental groups that may come with the new account. If given with
27 .B CAPNG_DROP_SUPP_GRP
28 it will have no effect.
29 .TP
30 .B CAPNG_APPLY_STAGED_GROUPS
31 Available in libcap-ng 0.9.3 and later.
32 Apply additional groups previously staged with
33 .BR capng_stage_additional_groups (3).
34 If used by itself, the staged list is applied exactly. If combined with
35 .B CAPNG_INIT_SUPP_GRP
36 the account's natural supplemental groups are initialized first and the
37 staged gids are then merged in without duplicates. If combined with
38 .B CAPNG_DROP_SUPP_GRP
39 the call fails. If this flag is set and no staged gids are present, the
40 call fails. Staged gids are intended to be used once and are always cleared before
41 .B capng_change_id
42 returns, even when the flag is omitted or the call fails.
43 .TP
44 .B CAPNG_CLEAR_BOUNDING
45 Clear the bounding set regardless to the internal representation already setup prior to changing the uid/gid.
46 .TP
47 .B CAPNG_APPLY_BOUNDING
48 Available in libcap-ng 0.9.3 and later.
49 Apply the internally prepared bounding set during
50 .BR capng_change_id .
51 This only acts on bounding-set state explicitly prepared with
52 .BR capng_clear (3),
53 .BR capng_fill (3),
54 or
55 .BR capng_update (3)
56 using
57 .BR CAPNG_BOUNDING_SET .
58 If no prepared bounding-set state exists, this flag is a no-op. If
59 combined with
60 .B CAPNG_CLEAR_BOUNDING
61 the call fails.
62 .TP
63 .B CAPNG_CLEAR_AMBIENT
64 Clear ambient capabilities regardless of the internal representation already setup prior to changing the uid/gid.
65
66 .RE
67 .SH "RETURN VALUE"
68
69 This returns 0 on success and a negative number on failure.
70 .RS
71 .TP
72 -1 means capng has not been initted properly
73 .TP
74 -2 means a failure requesting to keep capabilities across the uid change
75 .TP
76 -3 means that applying the intermediate capabilities failed
77 .TP
78 -4 means changing gid failed
79 .TP
80 -5 means initializing supplemental groups failed
81 .TP
82 -6 means changing the uid failed
83 .TP
84 -7 means dropping the ability to retain caps across a uid change failed
85 .TP
86 -8 means clearing the bounding set failed
87 .TP
88 -9 means dropping CAP_SETPCAP or ambient capabilities failed
89 .TP
90 -10 means resolving the target account for supplemental groups failed
91 .TP
92 -11 means dropping supplemental groups failed
93 .TP
94 -12 means CAPNG_DROP_SUPP_GRP and CAPNG_APPLY_STAGED_GROUPS were both set
95 .TP
96 -13 means CAPNG_APPLY_STAGED_GROUPS was requested with no staged gids
97 .TP
98 -14 means applying staged additional groups failed
99 .TP
100 -15 means looking up a group account in additional groups failed
101 .TP
102 -16 means merging initialized and staged additional groups failed
103 .TP
104 -17 means CAPNG_APPLY_BOUNDING and CAPNG_CLEAR_BOUNDING were both set
105 .RE
106
107 Applications that need to support libcap-ng releases older than 0.9.3
108 should detect the availability of
109 .B CAPNG_APPLY_BOUNDING
110 and
111 .B CAPNG_APPLY_STAGED_SUPP_GRP
112 and
113 .BR capng_stage_additional_groups (3)
114 before using this flow. Callers that need the prepared bounding-set
115 behavior must request it explicitly with
116 .B CAPNG_APPLY_BOUNDING
117 so older callers continue to see the historical
118 .B capng_change_id
119 behavior.
120
121 Note: the only safe action to do upon failure of this function is to probably exit. This is because you are likely in a situation with partial permissions and not what you intended.
122
123 .SH "SEE ALSO"
124
125 .BR capng_update (3),
126 .BR capng_apply (3),
127 .BR capng_stage_additional_groups (3),
128 .BR prctl (2),
129 .BR capabilities (7)
130
131 .SH AUTHOR
132 Steve Grubb