landlock_restrict_self.2: tfix
[man-pages.git] / man2 / setgid.2
blob6e1d4adb66ebdc19350400b2a543629a1b4831f9
1 .\" Copyright (C), 1994, Graeme W. Wilford. (Wilf.)
2 .\" and Copyright (C) 2010, 2015, Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .\" Fri Jul 29th 12:56:44 BST 1994  Wilf. <G.Wilford@ee.surrey.ac.uk>
7 .\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com>
8 .\" Modified 2002-03-09 by aeb
9 .\"
10 .TH SETGID 2 2021-03-22 "Linux" "Linux Programmer's Manual"
11 .SH NAME
12 setgid \- set group identity
13 .SH LIBRARY
14 Standard C library
15 .RI ( libc ", " \-lc )
16 .SH SYNOPSIS
17 .nf
18 .B #include <unistd.h>
19 .PP
20 .BI "int setgid(gid_t " gid );
21 .fi
22 .SH DESCRIPTION
23 .BR setgid ()
24 sets the effective group ID of the calling process.
25 If the calling process is privileged (more precisely: has the
26 .B CAP_SETGID
27 capability in its user namespace),
28 the real GID and saved set-group-ID are also set.
29 .PP
30 Under Linux,
31 .BR setgid ()
32 is implemented like the POSIX version with the
33 .B _POSIX_SAVED_IDS
34 feature.
35 This allows a set-group-ID program that is not set-user-ID-root
36 to drop all of its group
37 privileges, do some un-privileged work, and then reengage the original
38 effective group ID in a secure manner.
39 .SH RETURN VALUE
40 On success, zero is returned.
41 On error, \-1 is returned, and
42 .I errno
43 is set to indicate the error.
44 .SH ERRORS
45 .TP
46 .B EINVAL
47 The group ID specified in
48 .I gid
49 is not valid in this user namespace.
50 .TP
51 .B EPERM
52 The calling process is not privileged (does not have the
53 \fBCAP_SETGID\fP capability in its user namespace), and
54 .I gid
55 does not match the real group ID or saved set-group-ID of
56 the calling process.
57 .SH STANDARDS
58 POSIX.1-2001, POSIX.1-2008, SVr4.
59 .SH NOTES
60 The original Linux
61 .BR setgid ()
62 system call supported only 16-bit group IDs.
63 Subsequently, Linux 2.4 added
64 .BR setgid32 ()
65 supporting 32-bit IDs.
66 The glibc
67 .BR setgid ()
68 wrapper function transparently deals with the variation across kernel versions.
69 .\"
70 .SS C library/kernel differences
71 At the kernel level, user IDs and group IDs are a per-thread attribute.
72 However, POSIX requires that all threads in a process
73 share the same credentials.
74 The NPTL threading implementation handles the POSIX requirements by
75 providing wrapper functions for
76 the various system calls that change process UIDs and GIDs.
77 These wrapper functions (including the one for
78 .BR setgid ())
79 employ a signal-based technique to ensure
80 that when one thread changes credentials,
81 all of the other threads in the process also change their credentials.
82 For details, see
83 .BR nptl (7).
84 .SH SEE ALSO
85 .BR getgid (2),
86 .BR setegid (2),
87 .BR setregid (2),
88 .BR capabilities (7),
89 .BR credentials (7),
90 .BR user_namespaces (7)