2001-09-01 18:11:45 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2001 The FreeBSD Project
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-06-10 21:29:12 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2006-03-19 11:10:33 +00:00
|
|
|
#include "opt_compat.h"
|
|
|
|
|
2008-03-31 12:01:21 +00:00
|
|
|
#include <sys/fcntl.h>
|
2001-09-01 18:11:45 +00:00
|
|
|
#include <sys/param.h>
|
2002-04-13 23:11:23 +00:00
|
|
|
#include <sys/lock.h>
|
2002-09-05 08:13:20 +00:00
|
|
|
#include <sys/malloc.h>
|
2002-04-13 23:11:23 +00:00
|
|
|
#include <sys/mutex.h>
|
2006-11-06 13:42:10 +00:00
|
|
|
#include <sys/priv.h>
|
2001-09-01 18:11:45 +00:00
|
|
|
#include <sys/proc.h>
|
2002-09-01 22:30:27 +00:00
|
|
|
#include <sys/syscallsubr.h>
|
2001-09-01 18:11:45 +00:00
|
|
|
#include <sys/sysproto.h>
|
2002-09-05 08:13:20 +00:00
|
|
|
#include <sys/systm.h>
|
2001-09-01 18:11:45 +00:00
|
|
|
|
2005-01-14 04:44:56 +00:00
|
|
|
#ifdef COMPAT_LINUX32
|
2004-08-16 07:28:16 +00:00
|
|
|
#include <machine/../linux32/linux.h>
|
|
|
|
#include <machine/../linux32/linux32_proto.h>
|
2005-01-14 04:44:56 +00:00
|
|
|
#else
|
|
|
|
#include <machine/../linux/linux.h>
|
|
|
|
#include <machine/../linux/linux_proto.h>
|
2004-08-16 07:28:16 +00:00
|
|
|
#endif
|
2002-09-05 08:13:20 +00:00
|
|
|
|
2001-09-01 18:11:45 +00:00
|
|
|
#include <compat/linux/linux_util.h>
|
|
|
|
|
|
|
|
DUMMY(setfsuid16);
|
|
|
|
DUMMY(setfsgid16);
|
|
|
|
DUMMY(getresuid16);
|
|
|
|
DUMMY(getresgid16);
|
|
|
|
|
2002-09-01 22:30:27 +00:00
|
|
|
#define CAST_NOCHG(x) ((x == 0xFFFF) ? -1 : x)
|
2001-10-14 03:56:53 +00:00
|
|
|
|
2001-09-01 18:11:45 +00:00
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
linux_chown16(struct thread *td, struct linux_chown16_args *args)
|
2001-09-01 18:11:45 +00:00
|
|
|
{
|
2002-09-01 22:30:27 +00:00
|
|
|
char *path;
|
|
|
|
int error;
|
2001-09-01 18:11:45 +00:00
|
|
|
|
2002-09-01 22:30:27 +00:00
|
|
|
LCONVPATHEXIST(td, args->path, &path);
|
2001-09-01 18:11:45 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (ldebug(chown16))
|
2002-09-01 22:30:27 +00:00
|
|
|
printf(ARGS(chown16, "%s, %d, %d"), path, args->uid, args->gid);
|
2001-09-01 18:11:45 +00:00
|
|
|
#endif
|
2002-09-01 22:30:27 +00:00
|
|
|
error = kern_chown(td, path, UIO_SYSSPACE, CAST_NOCHG(args->uid),
|
|
|
|
CAST_NOCHG(args->gid));
|
|
|
|
LFREEPATH(path);
|
|
|
|
return (error);
|
2001-09-01 18:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
linux_lchown16(struct thread *td, struct linux_lchown16_args *args)
|
2001-09-01 18:11:45 +00:00
|
|
|
{
|
2002-09-01 22:30:27 +00:00
|
|
|
char *path;
|
|
|
|
int error;
|
2001-09-01 18:11:45 +00:00
|
|
|
|
2002-09-01 22:30:27 +00:00
|
|
|
LCONVPATHEXIST(td, args->path, &path);
|
2001-09-01 18:11:45 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (ldebug(lchown16))
|
2002-09-01 22:30:27 +00:00
|
|
|
printf(ARGS(lchown16, "%s, %d, %d"), path, args->uid,
|
2001-09-01 18:11:45 +00:00
|
|
|
args->gid);
|
|
|
|
#endif
|
2002-09-01 22:30:27 +00:00
|
|
|
error = kern_lchown(td, path, UIO_SYSSPACE, CAST_NOCHG(args->uid),
|
|
|
|
CAST_NOCHG(args->gid));
|
|
|
|
LFREEPATH(path);
|
|
|
|
return (error);
|
2001-09-01 18:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
linux_setgroups16(struct thread *td, struct linux_setgroups16_args *args)
|
2001-09-01 18:11:45 +00:00
|
|
|
{
|
|
|
|
struct ucred *newcred, *oldcred;
|
2009-06-19 17:10:35 +00:00
|
|
|
l_gid16_t *linux_gidset;
|
2001-09-01 18:11:45 +00:00
|
|
|
gid_t *bsd_gidset;
|
|
|
|
int ngrp, error;
|
2002-04-13 23:11:23 +00:00
|
|
|
struct proc *p;
|
2001-09-01 18:11:45 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (ldebug(setgroups16))
|
|
|
|
printf(ARGS(setgroups16, "%d, *"), args->gidsetsize);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ngrp = args->gidsetsize;
|
2010-01-12 07:49:34 +00:00
|
|
|
if (ngrp < 0 || ngrp > ngroups_max)
|
2002-04-13 23:11:23 +00:00
|
|
|
return (EINVAL);
|
2009-06-19 17:10:35 +00:00
|
|
|
linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_TEMP, M_WAITOK);
|
2003-03-03 09:14:26 +00:00
|
|
|
error = copyin(args->gidset, linux_gidset, ngrp * sizeof(l_gid16_t));
|
2002-04-13 23:11:23 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
newcred = crget();
|
|
|
|
p = td->td_proc;
|
|
|
|
PROC_LOCK(p);
|
2009-06-19 17:10:35 +00:00
|
|
|
oldcred = crcopysafe(p, newcred);
|
2001-09-01 18:11:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* cr_groups[0] holds egid. Setting the whole set from
|
|
|
|
* the supplied set will cause egid to be changed too.
|
|
|
|
* Keep cr_groups[0] unchanged to prevent that.
|
|
|
|
*/
|
|
|
|
|
2007-06-12 00:12:01 +00:00
|
|
|
if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) {
|
2002-04-13 23:11:23 +00:00
|
|
|
PROC_UNLOCK(p);
|
|
|
|
crfree(newcred);
|
2009-06-19 17:10:35 +00:00
|
|
|
goto out;
|
2002-04-13 23:11:23 +00:00
|
|
|
}
|
2001-09-01 18:11:45 +00:00
|
|
|
|
|
|
|
if (ngrp > 0) {
|
|
|
|
newcred->cr_ngroups = ngrp + 1;
|
|
|
|
|
|
|
|
bsd_gidset = newcred->cr_groups;
|
|
|
|
ngrp--;
|
|
|
|
while (ngrp >= 0) {
|
|
|
|
bsd_gidset[ngrp + 1] = linux_gidset[ngrp];
|
|
|
|
ngrp--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
newcred->cr_ngroups = 1;
|
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
setsugid(td->td_proc);
|
2002-04-13 23:11:23 +00:00
|
|
|
p->p_ucred = newcred;
|
|
|
|
PROC_UNLOCK(p);
|
2001-09-01 18:11:45 +00:00
|
|
|
crfree(oldcred);
|
2009-06-19 17:10:35 +00:00
|
|
|
error = 0;
|
|
|
|
out:
|
|
|
|
free(linux_gidset, M_TEMP);
|
|
|
|
return (error);
|
2001-09-01 18:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
linux_getgroups16(struct thread *td, struct linux_getgroups16_args *args)
|
2001-09-01 18:11:45 +00:00
|
|
|
{
|
|
|
|
struct ucred *cred;
|
2009-06-19 17:10:35 +00:00
|
|
|
l_gid16_t *linux_gidset;
|
2001-09-01 18:11:45 +00:00
|
|
|
gid_t *bsd_gidset;
|
|
|
|
int bsd_gidsetsz, ngrp, error;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (ldebug(getgroups16))
|
|
|
|
printf(ARGS(getgroups16, "%d, *"), args->gidsetsize);
|
|
|
|
#endif
|
|
|
|
|
2002-02-27 18:32:23 +00:00
|
|
|
cred = td->td_ucred;
|
2001-09-01 18:11:45 +00:00
|
|
|
bsd_gidset = cred->cr_groups;
|
|
|
|
bsd_gidsetsz = cred->cr_ngroups - 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* cr_groups[0] holds egid. Returning the whole set
|
|
|
|
* here will cause a duplicate. Exclude cr_groups[0]
|
|
|
|
* to prevent that.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((ngrp = args->gidsetsize) == 0) {
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_retval[0] = bsd_gidsetsz;
|
2001-09-01 18:11:45 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngrp < bsd_gidsetsz)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
ngrp = 0;
|
2009-06-19 17:10:35 +00:00
|
|
|
linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
|
|
|
|
M_TEMP, M_WAITOK);
|
2001-09-01 18:11:45 +00:00
|
|
|
while (ngrp < bsd_gidsetsz) {
|
|
|
|
linux_gidset[ngrp] = bsd_gidset[ngrp + 1];
|
|
|
|
ngrp++;
|
|
|
|
}
|
|
|
|
|
2003-03-03 09:14:26 +00:00
|
|
|
error = copyout(linux_gidset, args->gidset, ngrp * sizeof(l_gid16_t));
|
2009-06-19 17:10:35 +00:00
|
|
|
free(linux_gidset, M_TEMP);
|
2001-09-01 18:11:45 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_retval[0] = ngrp;
|
2001-09-01 18:11:45 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-09-12 08:38:13 +00:00
|
|
|
* The FreeBSD native getgid(2) and getuid(2) also modify td->td_retval[1]
|
2007-02-24 16:49:25 +00:00
|
|
|
* when COMPAT_43 is defined. This clobbers registers that are assumed to
|
|
|
|
* be preserved. The following lightweight syscalls fixes this. See also
|
|
|
|
* linux_getpid(2), linux_getgid(2) and linux_getuid(2) in linux_misc.c
|
2001-09-01 18:11:45 +00:00
|
|
|
*
|
|
|
|
* linux_getgid16() - MP SAFE
|
|
|
|
* linux_getuid16() - MP SAFE
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
linux_getgid16(struct thread *td, struct linux_getgid16_args *args)
|
2001-09-01 18:11:45 +00:00
|
|
|
{
|
2001-09-12 08:38:13 +00:00
|
|
|
|
2002-02-27 18:32:23 +00:00
|
|
|
td->td_retval[0] = td->td_ucred->cr_rgid;
|
2001-09-01 18:11:45 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
linux_getuid16(struct thread *td, struct linux_getuid16_args *args)
|
2001-09-01 18:11:45 +00:00
|
|
|
{
|
2001-09-12 08:38:13 +00:00
|
|
|
|
2002-02-27 18:32:23 +00:00
|
|
|
td->td_retval[0] = td->td_ucred->cr_ruid;
|
2001-09-01 18:11:45 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
linux_getegid16(struct thread *td, struct linux_getegid16_args *args)
|
2001-09-01 18:11:45 +00:00
|
|
|
{
|
|
|
|
struct getegid_args bsd;
|
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
return (getegid(td, &bsd));
|
2001-09-01 18:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
linux_geteuid16(struct thread *td, struct linux_geteuid16_args *args)
|
2001-09-01 18:11:45 +00:00
|
|
|
{
|
|
|
|
struct geteuid_args bsd;
|
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
return (geteuid(td, &bsd));
|
2001-09-01 18:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
linux_setgid16(struct thread *td, struct linux_setgid16_args *args)
|
2001-09-01 18:11:45 +00:00
|
|
|
{
|
|
|
|
struct setgid_args bsd;
|
|
|
|
|
|
|
|
bsd.gid = args->gid;
|
2001-09-12 08:38:13 +00:00
|
|
|
return (setgid(td, &bsd));
|
2001-09-01 18:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
linux_setuid16(struct thread *td, struct linux_setuid16_args *args)
|
2001-09-01 18:11:45 +00:00
|
|
|
{
|
|
|
|
struct setuid_args bsd;
|
|
|
|
|
|
|
|
bsd.uid = args->uid;
|
2001-09-12 08:38:13 +00:00
|
|
|
return (setuid(td, &bsd));
|
2001-09-01 18:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
linux_setregid16(struct thread *td, struct linux_setregid16_args *args)
|
2001-09-01 18:11:45 +00:00
|
|
|
{
|
|
|
|
struct setregid_args bsd;
|
|
|
|
|
2001-10-14 03:56:53 +00:00
|
|
|
bsd.rgid = CAST_NOCHG(args->rgid);
|
|
|
|
bsd.egid = CAST_NOCHG(args->egid);
|
2001-09-12 08:38:13 +00:00
|
|
|
return (setregid(td, &bsd));
|
2001-09-01 18:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
linux_setreuid16(struct thread *td, struct linux_setreuid16_args *args)
|
2001-09-01 18:11:45 +00:00
|
|
|
{
|
|
|
|
struct setreuid_args bsd;
|
|
|
|
|
2001-10-14 03:56:53 +00:00
|
|
|
bsd.ruid = CAST_NOCHG(args->ruid);
|
|
|
|
bsd.euid = CAST_NOCHG(args->euid);
|
2001-09-12 08:38:13 +00:00
|
|
|
return (setreuid(td, &bsd));
|
2001-09-01 18:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
linux_setresgid16(struct thread *td, struct linux_setresgid16_args *args)
|
2001-09-01 18:11:45 +00:00
|
|
|
{
|
|
|
|
struct setresgid_args bsd;
|
|
|
|
|
2001-10-14 03:56:53 +00:00
|
|
|
bsd.rgid = CAST_NOCHG(args->rgid);
|
|
|
|
bsd.egid = CAST_NOCHG(args->egid);
|
|
|
|
bsd.sgid = CAST_NOCHG(args->sgid);
|
2001-09-12 08:38:13 +00:00
|
|
|
return (setresgid(td, &bsd));
|
2001-09-01 18:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-09-12 08:38:13 +00:00
|
|
|
linux_setresuid16(struct thread *td, struct linux_setresuid16_args *args)
|
2001-09-01 18:11:45 +00:00
|
|
|
{
|
|
|
|
struct setresuid_args bsd;
|
|
|
|
|
2001-10-14 03:56:53 +00:00
|
|
|
bsd.ruid = CAST_NOCHG(args->ruid);
|
|
|
|
bsd.euid = CAST_NOCHG(args->euid);
|
|
|
|
bsd.suid = CAST_NOCHG(args->suid);
|
2001-09-12 08:38:13 +00:00
|
|
|
return (setresuid(td, &bsd));
|
2001-09-01 18:11:45 +00:00
|
|
|
}
|