1 /* Copyright (C) 1991-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
19 #include <hurd/msg_server.h>
24 _hurd_refport_secure_p (mach_port_t ref
)
26 if (ref
== __mach_task_self ())
28 if (__USEPORT (AUTH
, ref
== port
))
34 _S_msg_add_auth (mach_port_t me
,
39 uid_t
*genuids
, *gengids
, *auxuids
, *auxgids
;
40 mach_msg_type_number_t ngenuids
, ngengids
, nauxuids
, nauxgids
;
41 uid_t
*newgenuids
, *newgengids
, *newauxuids
, *newauxgids
;
42 mach_msg_type_number_t nnewgenuids
, nnewgengids
, nnewauxuids
, nnewauxgids
;
44 /* Create a list of ids and store it in NEWLISTP, length NEWLISTLEN.
45 Keep all the ids in EXIST (len NEXIST), adding in those from NEW
46 (len NNEW) which are not already there. */
47 error_t
make_list (uid_t
**newlistp
, mach_msg_type_number_t
*newlistlen
,
48 uid_t
*exist
, mach_msg_type_number_t nexist
,
49 uid_t
*new, mach_msg_type_number_t nnew
)
55 urp
= __vm_allocate (mach_task_self (), (vm_address_t
*) newlistp
,
56 nexist
+ nnew
* sizeof (uid_t
), 1);
61 for (i
= 0; i
< nexist
; i
++)
62 (*newlistp
)[j
++] = exist
[i
];
64 for (i
= 0; i
< nnew
; i
++)
66 for (k
= 0; k
< nexist
; k
++)
67 if (exist
[k
] == new[i
])
72 (*newlistp
)[j
++] = new[i
];
75 offset
= (round_page (nexist
+ nnew
* sizeof (uid_t
))
76 - round_page (j
* sizeof (uid_t
)));
78 __vm_deallocate (mach_task_self (),
79 (vm_address_t
) (*newlistp
80 + (nexist
+ nnew
* sizeof (uid_t
))),
86 /* Find out what ids ADDAUTH refers to */
88 genuids
= gengids
= auxuids
= auxgids
= 0;
89 ngenuids
= ngengids
= nauxuids
= nauxgids
= 0;
90 err
= __auth_getids (addauth
,
98 /* OR in these ids to what we already have, creating a new list. */
101 __mutex_lock (&_hurd_id
.lock
);
104 #define MAKE(genaux,uidgid) ({ \
105 new ## genaux ## uidgid ## s = 0; \
106 nnew ## genaux ## uidgid ## s = 0; \
107 make_list (&new ## genaux ## uidgid ## s, \
108 &nnew ## genaux ## uidgid ## s, \
109 _hurd_id.genaux.uidgid ## s, \
110 _hurd_id.genaux.n ## uidgid ## s, \
111 genaux ## uidgid ## s, \
112 n ## genaux ## uidgid ## s); \
115 err
= MAKE (gen
, uid
);
117 err
= MAKE (aux
, uid
);
119 err
= MAKE (gen
, gid
);
121 err
= MAKE (aux
, gid
);
124 __mutex_unlock (&_hurd_id
.lock
);
128 /* Create the new auth port */
131 err
= __USEPORT (AUTH
,
132 __auth_makeauth (port
,
133 &addauth
, MACH_MSG_TYPE_MOVE_SEND
, 1,
134 newgenuids
, nnewgenuids
,
135 newauxuids
, nnewauxuids
,
136 newgengids
, nnewgengids
,
137 newauxgids
, nnewauxgids
,
140 #define freeup(array, len) \
142 __vm_deallocate (mach_task_self (), (vm_address_t) array, \
143 len * sizeof (uid_t));
145 freeup (genuids
, ngenuids
);
146 freeup (auxuids
, nauxuids
);
147 freeup (gengids
, ngengids
);
148 freeup (auxgids
, nauxgids
);
149 freeup (newgenuids
, nnewgenuids
);
150 freeup (newauxuids
, nnewauxuids
);
151 freeup (newgengids
, nnewgengids
);
152 freeup (newauxgids
, nnewauxgids
);
158 /* And install it. */
160 err
= __setauth (newauth
);
161 __mach_port_deallocate (__mach_task_self (), newauth
);
169 _S_msg_del_auth (mach_port_t me
,
171 const_intarray_t uids
, mach_msg_type_number_t nuids
,
172 const_intarray_t gids
, mach_msg_type_number_t ngids
)
177 if (!_hurd_refport_secure_p (task
))
181 __mutex_lock (&_hurd_id
.lock
);
182 err
= _hurd_check_ids ();
187 size_t nu
= _hurd_id
.gen
.nuids
, ng
= _hurd_id
.gen
.ngids
;
191 memcpy (newu
, _hurd_id
.gen
.uids
, nu
* sizeof (uid_t
));
192 memcpy (newg
, _hurd_id
.gen
.gids
, ng
* sizeof (gid_t
));
194 for (j
= 0; j
< nuids
; ++j
)
196 const uid_t uid
= uids
[j
];
197 for (i
= 0; i
< nu
; ++i
)
199 /* Move the last uid into this slot, and decrease the
200 number of uids so the last slot is no longer used. */
201 newu
[i
] = newu
[--nu
];
203 __vm_deallocate (__mach_task_self (),
204 (vm_address_t
) uids
, nuids
* sizeof (uid_t
));
206 for (j
= 0; j
< ngids
; ++j
)
208 const gid_t gid
= gids
[j
];
209 for (i
= 0; i
< nu
; ++i
)
211 /* Move the last gid into this slot, and decrease the
212 number of gids so the last slot is no longer used. */
213 newu
[i
] = newu
[--nu
];
215 __vm_deallocate (__mach_task_self (),
216 (vm_address_t
) gids
, ngids
* sizeof (gid_t
));
218 err
= __USEPORT (AUTH
, __auth_makeauth
220 NULL
, MACH_MSG_TYPE_COPY_SEND
, 0,
222 _hurd_id
.aux
.uids
, _hurd_id
.aux
.nuids
,
224 _hurd_id
.aux
.uids
, _hurd_id
.aux
.ngids
,
227 __mutex_unlock (&_hurd_id
.lock
);
233 err
= __setauth (newauth
);
234 __mach_port_deallocate (__mach_task_self (), newauth
);