2019-05-27 06:55:01 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2007-04-26 22:48:28 +00:00
|
|
|
/* RxRPC key management
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
|
|
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
|
|
*
|
|
|
|
* RxRPC keys should have a description of describing their purpose:
|
2020-10-15 14:47:45 +00:00
|
|
|
* "afs@example.com"
|
2007-04-26 22:48:28 +00:00
|
|
|
*/
|
|
|
|
|
2016-06-02 19:08:52 +00:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2016-01-24 13:19:01 +00:00
|
|
|
#include <crypto/skcipher.h>
|
2007-04-26 22:48:28 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/net.h>
|
|
|
|
#include <linux/skbuff.h>
|
2007-10-17 06:29:46 +00:00
|
|
|
#include <linux/key-type.h>
|
2009-09-14 01:17:35 +00:00
|
|
|
#include <linux/ctype.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2007-04-26 22:48:28 +00:00
|
|
|
#include <net/sock.h>
|
|
|
|
#include <net/af_rxrpc.h>
|
|
|
|
#include <keys/rxrpc-type.h>
|
|
|
|
#include <keys/user-type.h>
|
|
|
|
#include "ar-internal.h"
|
|
|
|
|
2014-07-18 17:56:36 +00:00
|
|
|
static int rxrpc_preparse(struct key_preparsed_payload *);
|
|
|
|
static void rxrpc_free_preparse(struct key_preparsed_payload *);
|
2007-04-26 22:48:28 +00:00
|
|
|
static void rxrpc_destroy(struct key *);
|
|
|
|
static void rxrpc_describe(const struct key *, struct seq_file *);
|
2020-03-22 01:11:24 +00:00
|
|
|
static long rxrpc_read(const struct key *, char *, size_t);
|
2007-04-26 22:48:28 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* rxrpc defined keys take an arbitrary string as the description and an
|
|
|
|
* arbitrary blob of data as the payload
|
|
|
|
*/
|
|
|
|
struct key_type key_type_rxrpc = {
|
|
|
|
.name = "rxrpc",
|
2019-06-26 20:02:33 +00:00
|
|
|
.flags = KEY_TYPE_NET_DOMAIN,
|
2014-07-18 17:56:36 +00:00
|
|
|
.preparse = rxrpc_preparse,
|
|
|
|
.free_preparse = rxrpc_free_preparse,
|
|
|
|
.instantiate = generic_key_instantiate,
|
2007-04-26 22:48:28 +00:00
|
|
|
.destroy = rxrpc_destroy,
|
|
|
|
.describe = rxrpc_describe,
|
2009-09-14 01:17:40 +00:00
|
|
|
.read = rxrpc_read,
|
2007-04-26 22:48:28 +00:00
|
|
|
};
|
|
|
|
EXPORT_SYMBOL(key_type_rxrpc);
|
|
|
|
|
2009-09-14 01:17:35 +00:00
|
|
|
/*
|
|
|
|
* parse an RxKAD type XDR format token
|
|
|
|
* - the caller guarantees we have at least 4 words
|
|
|
|
*/
|
2014-07-18 17:56:36 +00:00
|
|
|
static int rxrpc_preparse_xdr_rxkad(struct key_preparsed_payload *prep,
|
|
|
|
size_t datalen,
|
|
|
|
const __be32 *xdr, unsigned int toklen)
|
2009-09-14 01:17:35 +00:00
|
|
|
{
|
2009-09-14 01:17:46 +00:00
|
|
|
struct rxrpc_key_token *token, **pptoken;
|
2017-08-29 09:15:40 +00:00
|
|
|
time64_t expiry;
|
2009-09-14 01:17:35 +00:00
|
|
|
size_t plen;
|
|
|
|
u32 tktlen;
|
|
|
|
|
|
|
|
_enter(",{%x,%x,%x,%x},%u",
|
|
|
|
ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
|
|
|
|
toklen);
|
|
|
|
|
|
|
|
if (toklen <= 8 * 4)
|
|
|
|
return -EKEYREJECTED;
|
|
|
|
tktlen = ntohl(xdr[7]);
|
|
|
|
_debug("tktlen: %x", tktlen);
|
|
|
|
if (tktlen > AFSTOKEN_RK_TIX_MAX)
|
|
|
|
return -EKEYREJECTED;
|
2014-05-15 14:51:22 +00:00
|
|
|
if (toklen < 8 * 4 + tktlen)
|
2009-09-14 01:17:35 +00:00
|
|
|
return -EKEYREJECTED;
|
|
|
|
|
|
|
|
plen = sizeof(*token) + sizeof(*token->kad) + tktlen;
|
2014-07-18 17:56:36 +00:00
|
|
|
prep->quotalen = datalen + plen;
|
2009-09-14 01:17:35 +00:00
|
|
|
|
|
|
|
plen -= sizeof(*token);
|
2011-02-25 15:33:17 +00:00
|
|
|
token = kzalloc(sizeof(*token), GFP_KERNEL);
|
2009-09-14 01:17:35 +00:00
|
|
|
if (!token)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2011-02-25 15:33:17 +00:00
|
|
|
token->kad = kzalloc(plen, GFP_KERNEL);
|
2009-09-14 01:17:35 +00:00
|
|
|
if (!token->kad) {
|
|
|
|
kfree(token);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
token->security_index = RXRPC_SECURITY_RXKAD;
|
|
|
|
token->kad->ticket_len = tktlen;
|
|
|
|
token->kad->vice_id = ntohl(xdr[0]);
|
|
|
|
token->kad->kvno = ntohl(xdr[1]);
|
|
|
|
token->kad->start = ntohl(xdr[4]);
|
|
|
|
token->kad->expiry = ntohl(xdr[5]);
|
|
|
|
token->kad->primary_flag = ntohl(xdr[6]);
|
|
|
|
memcpy(&token->kad->session_key, &xdr[2], 8);
|
|
|
|
memcpy(&token->kad->ticket, &xdr[8], tktlen);
|
|
|
|
|
|
|
|
_debug("SCIX: %u", token->security_index);
|
|
|
|
_debug("TLEN: %u", token->kad->ticket_len);
|
|
|
|
_debug("EXPY: %x", token->kad->expiry);
|
|
|
|
_debug("KVNO: %u", token->kad->kvno);
|
|
|
|
_debug("PRIM: %u", token->kad->primary_flag);
|
|
|
|
_debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
|
|
|
|
token->kad->session_key[0], token->kad->session_key[1],
|
|
|
|
token->kad->session_key[2], token->kad->session_key[3],
|
|
|
|
token->kad->session_key[4], token->kad->session_key[5],
|
|
|
|
token->kad->session_key[6], token->kad->session_key[7]);
|
|
|
|
if (token->kad->ticket_len >= 8)
|
|
|
|
_debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
|
|
|
|
token->kad->ticket[0], token->kad->ticket[1],
|
|
|
|
token->kad->ticket[2], token->kad->ticket[3],
|
|
|
|
token->kad->ticket[4], token->kad->ticket[5],
|
|
|
|
token->kad->ticket[6], token->kad->ticket[7]);
|
|
|
|
|
|
|
|
/* count the number of tokens attached */
|
2015-10-21 13:04:48 +00:00
|
|
|
prep->payload.data[1] = (void *)((unsigned long)prep->payload.data[1] + 1);
|
2009-09-14 01:17:35 +00:00
|
|
|
|
|
|
|
/* attach the data */
|
2015-10-21 13:04:48 +00:00
|
|
|
for (pptoken = (struct rxrpc_key_token **)&prep->payload.data[0];
|
2009-09-14 01:17:46 +00:00
|
|
|
*pptoken;
|
|
|
|
pptoken = &(*pptoken)->next)
|
|
|
|
continue;
|
|
|
|
*pptoken = token;
|
2017-08-29 09:15:40 +00:00
|
|
|
expiry = rxrpc_u32_to_time64(token->kad->expiry);
|
|
|
|
if (expiry < prep->expiry)
|
|
|
|
prep->expiry = expiry;
|
2009-09-14 01:17:46 +00:00
|
|
|
|
|
|
|
_leave(" = 0");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-14 01:17:35 +00:00
|
|
|
/*
|
|
|
|
* attempt to parse the data as the XDR format
|
|
|
|
* - the caller guarantees we have more than 7 words
|
|
|
|
*/
|
2014-07-18 17:56:36 +00:00
|
|
|
static int rxrpc_preparse_xdr(struct key_preparsed_payload *prep)
|
2009-09-14 01:17:35 +00:00
|
|
|
{
|
2020-09-27 10:17:03 +00:00
|
|
|
const __be32 *xdr = prep->data, *token, *p;
|
2009-09-14 01:17:35 +00:00
|
|
|
const char *cp;
|
2017-06-14 23:12:24 +00:00
|
|
|
unsigned int len, paddedlen, loop, ntoken, toklen, sec_ix;
|
2014-07-18 17:56:36 +00:00
|
|
|
size_t datalen = prep->datalen;
|
2020-10-15 14:59:36 +00:00
|
|
|
int ret, ret2;
|
2009-09-14 01:17:35 +00:00
|
|
|
|
|
|
|
_enter(",{%x,%x,%x,%x},%zu",
|
|
|
|
ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
|
2014-07-18 17:56:36 +00:00
|
|
|
prep->datalen);
|
2009-09-14 01:17:35 +00:00
|
|
|
|
|
|
|
if (datalen > AFSTOKEN_LENGTH_MAX)
|
|
|
|
goto not_xdr;
|
|
|
|
|
|
|
|
/* XDR is an array of __be32's */
|
|
|
|
if (datalen & 3)
|
|
|
|
goto not_xdr;
|
|
|
|
|
|
|
|
/* the flags should be 0 (the setpag bit must be handled by
|
|
|
|
* userspace) */
|
|
|
|
if (ntohl(*xdr++) != 0)
|
|
|
|
goto not_xdr;
|
|
|
|
datalen -= 4;
|
|
|
|
|
|
|
|
/* check the cell name */
|
|
|
|
len = ntohl(*xdr++);
|
|
|
|
if (len < 1 || len > AFSTOKEN_CELL_MAX)
|
|
|
|
goto not_xdr;
|
|
|
|
datalen -= 4;
|
2017-06-14 23:12:24 +00:00
|
|
|
paddedlen = (len + 3) & ~3;
|
|
|
|
if (paddedlen > datalen)
|
2009-09-14 01:17:35 +00:00
|
|
|
goto not_xdr;
|
|
|
|
|
|
|
|
cp = (const char *) xdr;
|
|
|
|
for (loop = 0; loop < len; loop++)
|
|
|
|
if (!isprint(cp[loop]))
|
|
|
|
goto not_xdr;
|
2017-06-14 23:12:24 +00:00
|
|
|
for (; loop < paddedlen; loop++)
|
|
|
|
if (cp[loop])
|
|
|
|
goto not_xdr;
|
2009-09-14 01:17:35 +00:00
|
|
|
_debug("cellname: [%u/%u] '%*.*s'",
|
2017-06-14 23:12:24 +00:00
|
|
|
len, paddedlen, len, len, (const char *) xdr);
|
|
|
|
datalen -= paddedlen;
|
|
|
|
xdr += paddedlen >> 2;
|
2009-09-14 01:17:35 +00:00
|
|
|
|
|
|
|
/* get the token count */
|
|
|
|
if (datalen < 12)
|
|
|
|
goto not_xdr;
|
|
|
|
ntoken = ntohl(*xdr++);
|
|
|
|
datalen -= 4;
|
|
|
|
_debug("ntoken: %x", ntoken);
|
|
|
|
if (ntoken < 1 || ntoken > AFSTOKEN_MAX)
|
|
|
|
goto not_xdr;
|
|
|
|
|
|
|
|
/* check each token wrapper */
|
2020-09-27 10:17:03 +00:00
|
|
|
p = xdr;
|
2009-09-14 01:17:35 +00:00
|
|
|
loop = ntoken;
|
|
|
|
do {
|
|
|
|
if (datalen < 8)
|
|
|
|
goto not_xdr;
|
2020-09-27 10:17:03 +00:00
|
|
|
toklen = ntohl(*p++);
|
|
|
|
sec_ix = ntohl(*p);
|
2009-09-14 01:17:35 +00:00
|
|
|
datalen -= 4;
|
|
|
|
_debug("token: [%x/%zx] %x", toklen, datalen, sec_ix);
|
2017-06-14 23:12:24 +00:00
|
|
|
paddedlen = (toklen + 3) & ~3;
|
|
|
|
if (toklen < 20 || toklen > datalen || paddedlen > datalen)
|
2009-09-14 01:17:35 +00:00
|
|
|
goto not_xdr;
|
2017-06-14 23:12:24 +00:00
|
|
|
datalen -= paddedlen;
|
2020-09-27 10:17:03 +00:00
|
|
|
p += paddedlen >> 2;
|
2009-09-14 01:17:35 +00:00
|
|
|
|
|
|
|
} while (--loop > 0);
|
|
|
|
|
|
|
|
_debug("remainder: %zu", datalen);
|
|
|
|
if (datalen != 0)
|
|
|
|
goto not_xdr;
|
|
|
|
|
|
|
|
/* okay: we're going to assume it's valid XDR format
|
|
|
|
* - we ignore the cellname, relying on the key to be correctly named
|
|
|
|
*/
|
2020-10-15 14:59:36 +00:00
|
|
|
ret = -EPROTONOSUPPORT;
|
2009-09-14 01:17:35 +00:00
|
|
|
do {
|
|
|
|
toklen = ntohl(*xdr++);
|
2020-09-27 10:17:03 +00:00
|
|
|
token = xdr;
|
|
|
|
xdr += (toklen + 3) / 4;
|
|
|
|
|
|
|
|
sec_ix = ntohl(*token++);
|
2009-09-14 01:17:35 +00:00
|
|
|
toklen -= 4;
|
|
|
|
|
2020-09-27 10:17:03 +00:00
|
|
|
_debug("TOKEN type=%x len=%x", sec_ix, toklen);
|
2009-09-14 01:17:46 +00:00
|
|
|
|
2009-09-14 01:17:35 +00:00
|
|
|
switch (sec_ix) {
|
|
|
|
case RXRPC_SECURITY_RXKAD:
|
2020-10-15 14:59:36 +00:00
|
|
|
ret2 = rxrpc_preparse_xdr_rxkad(prep, datalen, token, toklen);
|
2009-09-14 01:17:35 +00:00
|
|
|
break;
|
2020-10-15 14:59:36 +00:00
|
|
|
default:
|
|
|
|
ret2 = -EPROTONOSUPPORT;
|
|
|
|
break;
|
|
|
|
}
|
2009-09-14 01:17:35 +00:00
|
|
|
|
2020-10-15 14:59:36 +00:00
|
|
|
switch (ret2) {
|
|
|
|
case 0:
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
case -EPROTONOSUPPORT:
|
|
|
|
break;
|
|
|
|
case -ENOPKG:
|
|
|
|
if (ret != 0)
|
|
|
|
ret = -ENOPKG;
|
|
|
|
break;
|
2009-09-14 01:17:35 +00:00
|
|
|
default:
|
2020-10-15 14:59:36 +00:00
|
|
|
ret = ret2;
|
2009-09-14 01:17:35 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (--ntoken > 0);
|
|
|
|
|
2020-10-15 14:59:36 +00:00
|
|
|
error:
|
|
|
|
_leave(" = %d", ret);
|
|
|
|
return ret;
|
2009-09-14 01:17:35 +00:00
|
|
|
|
|
|
|
not_xdr:
|
|
|
|
_leave(" = -EPROTO");
|
|
|
|
return -EPROTO;
|
|
|
|
}
|
|
|
|
|
2007-04-26 22:48:28 +00:00
|
|
|
/*
|
2014-07-18 17:56:36 +00:00
|
|
|
* Preparse an rxrpc defined key.
|
|
|
|
*
|
|
|
|
* Data should be of the form:
|
2007-04-26 22:48:28 +00:00
|
|
|
* OFFSET LEN CONTENT
|
|
|
|
* 0 4 key interface version number
|
|
|
|
* 4 2 security index (type)
|
|
|
|
* 6 2 ticket length
|
|
|
|
* 8 4 key expiry time (time_t)
|
|
|
|
* 12 4 kvno
|
|
|
|
* 16 8 session key
|
|
|
|
* 24 [len] ticket
|
|
|
|
*
|
|
|
|
* if no data is provided, then a no-security key is made
|
|
|
|
*/
|
2014-07-18 17:56:36 +00:00
|
|
|
static int rxrpc_preparse(struct key_preparsed_payload *prep)
|
2007-04-26 22:48:28 +00:00
|
|
|
{
|
2009-09-14 01:17:35 +00:00
|
|
|
const struct rxrpc_key_data_v1 *v1;
|
|
|
|
struct rxrpc_key_token *token, **pp;
|
2017-08-29 09:15:40 +00:00
|
|
|
time64_t expiry;
|
2007-04-26 22:48:28 +00:00
|
|
|
size_t plen;
|
|
|
|
u32 kver;
|
|
|
|
int ret;
|
|
|
|
|
2014-07-18 17:56:36 +00:00
|
|
|
_enter("%zu", prep->datalen);
|
2007-04-26 22:48:28 +00:00
|
|
|
|
|
|
|
/* handle a no-security key */
|
2012-09-13 12:06:29 +00:00
|
|
|
if (!prep->data && prep->datalen == 0)
|
2007-04-26 22:48:28 +00:00
|
|
|
return 0;
|
|
|
|
|
2009-09-14 01:17:35 +00:00
|
|
|
/* determine if the XDR payload format is being used */
|
2012-09-13 12:06:29 +00:00
|
|
|
if (prep->datalen > 7 * 4) {
|
2014-07-18 17:56:36 +00:00
|
|
|
ret = rxrpc_preparse_xdr(prep);
|
2009-09-14 01:17:35 +00:00
|
|
|
if (ret != -EPROTO)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-04-26 22:48:28 +00:00
|
|
|
/* get the key interface version number */
|
|
|
|
ret = -EINVAL;
|
2012-09-13 12:06:29 +00:00
|
|
|
if (prep->datalen <= 4 || !prep->data)
|
2007-04-26 22:48:28 +00:00
|
|
|
goto error;
|
2012-09-13 12:06:29 +00:00
|
|
|
memcpy(&kver, prep->data, sizeof(kver));
|
|
|
|
prep->data += sizeof(kver);
|
|
|
|
prep->datalen -= sizeof(kver);
|
2007-04-26 22:48:28 +00:00
|
|
|
|
|
|
|
_debug("KEY I/F VERSION: %u", kver);
|
|
|
|
|
|
|
|
ret = -EKEYREJECTED;
|
|
|
|
if (kver != 1)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* deal with a version 1 key */
|
|
|
|
ret = -EINVAL;
|
2012-09-13 12:06:29 +00:00
|
|
|
if (prep->datalen < sizeof(*v1))
|
2007-04-26 22:48:28 +00:00
|
|
|
goto error;
|
|
|
|
|
2012-09-13 12:06:29 +00:00
|
|
|
v1 = prep->data;
|
|
|
|
if (prep->datalen != sizeof(*v1) + v1->ticket_length)
|
2007-04-26 22:48:28 +00:00
|
|
|
goto error;
|
|
|
|
|
2009-09-14 01:17:35 +00:00
|
|
|
_debug("SCIX: %u", v1->security_index);
|
|
|
|
_debug("TLEN: %u", v1->ticket_length);
|
|
|
|
_debug("EXPY: %x", v1->expiry);
|
|
|
|
_debug("KVNO: %u", v1->kvno);
|
2007-04-26 22:48:28 +00:00
|
|
|
_debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
|
2009-09-14 01:17:35 +00:00
|
|
|
v1->session_key[0], v1->session_key[1],
|
|
|
|
v1->session_key[2], v1->session_key[3],
|
|
|
|
v1->session_key[4], v1->session_key[5],
|
|
|
|
v1->session_key[6], v1->session_key[7]);
|
|
|
|
if (v1->ticket_length >= 8)
|
2007-04-26 22:48:28 +00:00
|
|
|
_debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
|
2009-09-14 01:17:35 +00:00
|
|
|
v1->ticket[0], v1->ticket[1],
|
|
|
|
v1->ticket[2], v1->ticket[3],
|
|
|
|
v1->ticket[4], v1->ticket[5],
|
|
|
|
v1->ticket[6], v1->ticket[7]);
|
2007-04-26 22:48:28 +00:00
|
|
|
|
|
|
|
ret = -EPROTONOSUPPORT;
|
2009-09-14 01:17:35 +00:00
|
|
|
if (v1->security_index != RXRPC_SECURITY_RXKAD)
|
2007-04-26 22:48:28 +00:00
|
|
|
goto error;
|
|
|
|
|
2009-09-14 01:17:35 +00:00
|
|
|
plen = sizeof(*token->kad) + v1->ticket_length;
|
2014-07-18 17:56:36 +00:00
|
|
|
prep->quotalen = plen + sizeof(*token);
|
2007-04-26 22:48:28 +00:00
|
|
|
|
|
|
|
ret = -ENOMEM;
|
2011-02-25 15:33:17 +00:00
|
|
|
token = kzalloc(sizeof(*token), GFP_KERNEL);
|
2009-09-14 01:17:35 +00:00
|
|
|
if (!token)
|
2007-04-26 22:48:28 +00:00
|
|
|
goto error;
|
2011-02-25 15:33:17 +00:00
|
|
|
token->kad = kzalloc(plen, GFP_KERNEL);
|
2009-09-14 01:17:35 +00:00
|
|
|
if (!token->kad)
|
|
|
|
goto error_free;
|
|
|
|
|
|
|
|
token->security_index = RXRPC_SECURITY_RXKAD;
|
|
|
|
token->kad->ticket_len = v1->ticket_length;
|
|
|
|
token->kad->expiry = v1->expiry;
|
|
|
|
token->kad->kvno = v1->kvno;
|
|
|
|
memcpy(&token->kad->session_key, &v1->session_key, 8);
|
|
|
|
memcpy(&token->kad->ticket, v1->ticket, v1->ticket_length);
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2014-07-18 17:56:36 +00:00
|
|
|
/* count the number of tokens attached */
|
2015-10-21 13:04:48 +00:00
|
|
|
prep->payload.data[1] = (void *)((unsigned long)prep->payload.data[1] + 1);
|
2009-09-14 01:17:35 +00:00
|
|
|
|
2014-07-18 17:56:36 +00:00
|
|
|
/* attach the data */
|
2015-10-21 13:04:48 +00:00
|
|
|
pp = (struct rxrpc_key_token **)&prep->payload.data[0];
|
2009-09-14 01:17:35 +00:00
|
|
|
while (*pp)
|
|
|
|
pp = &(*pp)->next;
|
|
|
|
*pp = token;
|
2017-08-29 09:15:40 +00:00
|
|
|
expiry = rxrpc_u32_to_time64(token->kad->expiry);
|
|
|
|
if (expiry < prep->expiry)
|
|
|
|
prep->expiry = expiry;
|
2009-09-14 01:17:35 +00:00
|
|
|
token = NULL;
|
2007-04-26 22:48:28 +00:00
|
|
|
ret = 0;
|
|
|
|
|
2009-09-14 01:17:35 +00:00
|
|
|
error_free:
|
|
|
|
kfree(token);
|
2007-04-26 22:48:28 +00:00
|
|
|
error:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-07-18 17:56:36 +00:00
|
|
|
* Free token list.
|
2007-04-26 22:48:28 +00:00
|
|
|
*/
|
2014-07-18 17:56:36 +00:00
|
|
|
static void rxrpc_free_token_list(struct rxrpc_key_token *token)
|
|
|
|
{
|
|
|
|
struct rxrpc_key_token *next;
|
|
|
|
|
|
|
|
for (; token; token = next) {
|
|
|
|
next = token->next;
|
|
|
|
switch (token->security_index) {
|
|
|
|
case RXRPC_SECURITY_RXKAD:
|
|
|
|
kfree(token->kad);
|
|
|
|
break;
|
|
|
|
default:
|
2016-06-02 19:08:52 +00:00
|
|
|
pr_err("Unknown token type %x on rxrpc key\n",
|
2014-07-18 17:56:36 +00:00
|
|
|
token->security_index);
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
|
|
|
|
kfree(token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clean up preparse data.
|
|
|
|
*/
|
|
|
|
static void rxrpc_free_preparse(struct key_preparsed_payload *prep)
|
|
|
|
{
|
2015-10-21 13:04:48 +00:00
|
|
|
rxrpc_free_token_list(prep->payload.data[0]);
|
2014-07-18 17:56:36 +00:00
|
|
|
}
|
|
|
|
|
2007-04-26 22:48:28 +00:00
|
|
|
/*
|
|
|
|
* dispose of the data dangling from the corpse of a rxrpc key
|
|
|
|
*/
|
|
|
|
static void rxrpc_destroy(struct key *key)
|
|
|
|
{
|
2015-10-21 13:04:48 +00:00
|
|
|
rxrpc_free_token_list(key->payload.data[0]);
|
2007-04-26 22:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* describe the rxrpc key
|
|
|
|
*/
|
|
|
|
static void rxrpc_describe(const struct key *key, struct seq_file *m)
|
2020-09-08 21:30:52 +00:00
|
|
|
{
|
|
|
|
const struct rxrpc_key_token *token;
|
|
|
|
const char *sep = ": ";
|
|
|
|
|
|
|
|
seq_puts(m, key->description);
|
|
|
|
|
|
|
|
for (token = key->payload.data[0]; token; token = token->next) {
|
|
|
|
seq_puts(m, sep);
|
|
|
|
|
|
|
|
switch (token->security_index) {
|
|
|
|
case RXRPC_SECURITY_RXKAD:
|
|
|
|
seq_puts(m, "ka");
|
|
|
|
break;
|
|
|
|
default: /* we have a ticket we can't encode */
|
|
|
|
seq_printf(m, "%u", token->security_index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sep = " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-26 22:48:28 +00:00
|
|
|
/*
|
|
|
|
* grab the security key for a socket
|
|
|
|
*/
|
2020-07-23 06:09:07 +00:00
|
|
|
int rxrpc_request_key(struct rxrpc_sock *rx, sockptr_t optval, int optlen)
|
2007-04-26 22:48:28 +00:00
|
|
|
{
|
|
|
|
struct key *key;
|
|
|
|
char *description;
|
|
|
|
|
|
|
|
_enter("");
|
|
|
|
|
2020-10-02 13:04:51 +00:00
|
|
|
if (optlen <= 0 || optlen > PAGE_SIZE - 1 || rx->securities)
|
2007-04-26 22:48:28 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2020-07-23 06:09:07 +00:00
|
|
|
description = memdup_sockptr_nul(optval, optlen);
|
2015-12-24 05:06:05 +00:00
|
|
|
if (IS_ERR(description))
|
|
|
|
return PTR_ERR(description);
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2019-07-11 01:43:43 +00:00
|
|
|
key = request_key_net(&key_type_rxrpc, description, sock_net(&rx->sk), NULL);
|
2007-04-26 22:48:28 +00:00
|
|
|
if (IS_ERR(key)) {
|
|
|
|
kfree(description);
|
|
|
|
_leave(" = %ld", PTR_ERR(key));
|
|
|
|
return PTR_ERR(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
rx->key = key;
|
|
|
|
kfree(description);
|
|
|
|
_leave(" = 0 [key %x]", key->serial);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* generate a server data key
|
|
|
|
*/
|
|
|
|
int rxrpc_get_server_data_key(struct rxrpc_connection *conn,
|
|
|
|
const void *session_key,
|
2017-08-29 09:15:40 +00:00
|
|
|
time64_t expiry,
|
2007-04-26 22:48:28 +00:00
|
|
|
u32 kvno)
|
|
|
|
{
|
CRED: Inaugurate COW credentials
Inaugurate copy-on-write credentials management. This uses RCU to manage the
credentials pointer in the task_struct with respect to accesses by other tasks.
A process may only modify its own credentials, and so does not need locking to
access or modify its own credentials.
A mutex (cred_replace_mutex) is added to the task_struct to control the effect
of PTRACE_ATTACHED on credential calculations, particularly with respect to
execve().
With this patch, the contents of an active credentials struct may not be
changed directly; rather a new set of credentials must be prepared, modified
and committed using something like the following sequence of events:
struct cred *new = prepare_creds();
int ret = blah(new);
if (ret < 0) {
abort_creds(new);
return ret;
}
return commit_creds(new);
There are some exceptions to this rule: the keyrings pointed to by the active
credentials may be instantiated - keyrings violate the COW rule as managing
COW keyrings is tricky, given that it is possible for a task to directly alter
the keys in a keyring in use by another task.
To help enforce this, various pointers to sets of credentials, such as those in
the task_struct, are declared const. The purpose of this is compile-time
discouragement of altering credentials through those pointers. Once a set of
credentials has been made public through one of these pointers, it may not be
modified, except under special circumstances:
(1) Its reference count may incremented and decremented.
(2) The keyrings to which it points may be modified, but not replaced.
The only safe way to modify anything else is to create a replacement and commit
using the functions described in Documentation/credentials.txt (which will be
added by a later patch).
This patch and the preceding patches have been tested with the LTP SELinux
testsuite.
This patch makes several logical sets of alteration:
(1) execve().
This now prepares and commits credentials in various places in the
security code rather than altering the current creds directly.
(2) Temporary credential overrides.
do_coredump() and sys_faccessat() now prepare their own credentials and
temporarily override the ones currently on the acting thread, whilst
preventing interference from other threads by holding cred_replace_mutex
on the thread being dumped.
This will be replaced in a future patch by something that hands down the
credentials directly to the functions being called, rather than altering
the task's objective credentials.
(3) LSM interface.
A number of functions have been changed, added or removed:
(*) security_capset_check(), ->capset_check()
(*) security_capset_set(), ->capset_set()
Removed in favour of security_capset().
(*) security_capset(), ->capset()
New. This is passed a pointer to the new creds, a pointer to the old
creds and the proposed capability sets. It should fill in the new
creds or return an error. All pointers, barring the pointer to the
new creds, are now const.
(*) security_bprm_apply_creds(), ->bprm_apply_creds()
Changed; now returns a value, which will cause the process to be
killed if it's an error.
(*) security_task_alloc(), ->task_alloc_security()
Removed in favour of security_prepare_creds().
(*) security_cred_free(), ->cred_free()
New. Free security data attached to cred->security.
(*) security_prepare_creds(), ->cred_prepare()
New. Duplicate any security data attached to cred->security.
(*) security_commit_creds(), ->cred_commit()
New. Apply any security effects for the upcoming installation of new
security by commit_creds().
(*) security_task_post_setuid(), ->task_post_setuid()
Removed in favour of security_task_fix_setuid().
(*) security_task_fix_setuid(), ->task_fix_setuid()
Fix up the proposed new credentials for setuid(). This is used by
cap_set_fix_setuid() to implicitly adjust capabilities in line with
setuid() changes. Changes are made to the new credentials, rather
than the task itself as in security_task_post_setuid().
(*) security_task_reparent_to_init(), ->task_reparent_to_init()
Removed. Instead the task being reparented to init is referred
directly to init's credentials.
NOTE! This results in the loss of some state: SELinux's osid no
longer records the sid of the thread that forked it.
(*) security_key_alloc(), ->key_alloc()
(*) security_key_permission(), ->key_permission()
Changed. These now take cred pointers rather than task pointers to
refer to the security context.
(4) sys_capset().
This has been simplified and uses less locking. The LSM functions it
calls have been merged.
(5) reparent_to_kthreadd().
This gives the current thread the same credentials as init by simply using
commit_thread() to point that way.
(6) __sigqueue_alloc() and switch_uid()
__sigqueue_alloc() can't stop the target task from changing its creds
beneath it, so this function gets a reference to the currently applicable
user_struct which it then passes into the sigqueue struct it returns if
successful.
switch_uid() is now called from commit_creds(), and possibly should be
folded into that. commit_creds() should take care of protecting
__sigqueue_alloc().
(7) [sg]et[ug]id() and co and [sg]et_current_groups.
The set functions now all use prepare_creds(), commit_creds() and
abort_creds() to build and check a new set of credentials before applying
it.
security_task_set[ug]id() is called inside the prepared section. This
guarantees that nothing else will affect the creds until we've finished.
The calling of set_dumpable() has been moved into commit_creds().
Much of the functionality of set_user() has been moved into
commit_creds().
The get functions all simply access the data directly.
(8) security_task_prctl() and cap_task_prctl().
security_task_prctl() has been modified to return -ENOSYS if it doesn't
want to handle a function, or otherwise return the return value directly
rather than through an argument.
Additionally, cap_task_prctl() now prepares a new set of credentials, even
if it doesn't end up using it.
(9) Keyrings.
A number of changes have been made to the keyrings code:
(a) switch_uid_keyring(), copy_keys(), exit_keys() and suid_keys() have
all been dropped and built in to the credentials functions directly.
They may want separating out again later.
(b) key_alloc() and search_process_keyrings() now take a cred pointer
rather than a task pointer to specify the security context.
(c) copy_creds() gives a new thread within the same thread group a new
thread keyring if its parent had one, otherwise it discards the thread
keyring.
(d) The authorisation key now points directly to the credentials to extend
the search into rather pointing to the task that carries them.
(e) Installing thread, process or session keyrings causes a new set of
credentials to be created, even though it's not strictly necessary for
process or session keyrings (they're shared).
(10) Usermode helper.
The usermode helper code now carries a cred struct pointer in its
subprocess_info struct instead of a new session keyring pointer. This set
of credentials is derived from init_cred and installed on the new process
after it has been cloned.
call_usermodehelper_setup() allocates the new credentials and
call_usermodehelper_freeinfo() discards them if they haven't been used. A
special cred function (prepare_usermodeinfo_creds()) is provided
specifically for call_usermodehelper_setup() to call.
call_usermodehelper_setkeys() adjusts the credentials to sport the
supplied keyring as the new session keyring.
(11) SELinux.
SELinux has a number of changes, in addition to those to support the LSM
interface changes mentioned above:
(a) selinux_setprocattr() no longer does its check for whether the
current ptracer can access processes with the new SID inside the lock
that covers getting the ptracer's SID. Whilst this lock ensures that
the check is done with the ptracer pinned, the result is only valid
until the lock is released, so there's no point doing it inside the
lock.
(12) is_single_threaded().
This function has been extracted from selinux_setprocattr() and put into
a file of its own in the lib/ directory as join_session_keyring() now
wants to use it too.
The code in SELinux just checked to see whether a task shared mm_structs
with other tasks (CLONE_VM), but that isn't good enough. We really want
to know if they're part of the same thread group (CLONE_THREAD).
(13) nfsd.
The NFS server daemon now has to use the COW credentials to set the
credentials it is going to use. It really needs to pass the credentials
down to the functions it calls, but it can't do that until other patches
in this series have been applied.
Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: James Morris <jmorris@namei.org>
Signed-off-by: James Morris <jmorris@namei.org>
2008-11-13 23:39:23 +00:00
|
|
|
const struct cred *cred = current_cred();
|
2007-04-26 22:48:28 +00:00
|
|
|
struct key *key;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
u32 kver;
|
2009-09-14 01:17:35 +00:00
|
|
|
struct rxrpc_key_data_v1 v1;
|
2007-04-26 22:48:28 +00:00
|
|
|
} data;
|
|
|
|
|
|
|
|
_enter("");
|
|
|
|
|
2012-05-25 22:37:54 +00:00
|
|
|
key = key_alloc(&key_type_rxrpc, "x",
|
2019-07-11 01:43:43 +00:00
|
|
|
GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred, 0,
|
KEYS: Add a facility to restrict new links into a keyring
Add a facility whereby proposed new links to be added to a keyring can be
vetted, permitting them to be rejected if necessary. This can be used to
block public keys from which the signature cannot be verified or for which
the signature verification fails. It could also be used to provide
blacklisting.
This affects operations like add_key(), KEYCTL_LINK and KEYCTL_INSTANTIATE.
To this end:
(1) A function pointer is added to the key struct that, if set, points to
the vetting function. This is called as:
int (*restrict_link)(struct key *keyring,
const struct key_type *key_type,
unsigned long key_flags,
const union key_payload *key_payload),
where 'keyring' will be the keyring being added to, key_type and
key_payload will describe the key being added and key_flags[*] can be
AND'ed with KEY_FLAG_TRUSTED.
[*] This parameter will be removed in a later patch when
KEY_FLAG_TRUSTED is removed.
The function should return 0 to allow the link to take place or an
error (typically -ENOKEY, -ENOPKG or -EKEYREJECTED) to reject the
link.
The pointer should not be set directly, but rather should be set
through keyring_alloc().
Note that if called during add_key(), preparse is called before this
method, but a key isn't actually allocated until after this function
is called.
(2) KEY_ALLOC_BYPASS_RESTRICTION is added. This can be passed to
key_create_or_update() or key_instantiate_and_link() to bypass the
restriction check.
(3) KEY_FLAG_TRUSTED_ONLY is removed. The entire contents of a keyring
with this restriction emplaced can be considered 'trustworthy' by
virtue of being in the keyring when that keyring is consulted.
(4) key_alloc() and keyring_alloc() take an extra argument that will be
used to set restrict_link in the new key. This ensures that the
pointer is set before the key is published, thus preventing a window
of unrestrictedness. Normally this argument will be NULL.
(5) As a temporary affair, keyring_restrict_trusted_only() is added. It
should be passed to keyring_alloc() as the extra argument instead of
setting KEY_FLAG_TRUSTED_ONLY on a keyring. This will be replaced in
a later patch with functions that look in the appropriate places for
authoritative keys.
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Mimi Zohar <zohar@linux.vnet.ibm.com>
2016-04-06 15:14:24 +00:00
|
|
|
KEY_ALLOC_NOT_IN_QUOTA, NULL);
|
2007-04-26 22:48:28 +00:00
|
|
|
if (IS_ERR(key)) {
|
|
|
|
_leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key));
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
_debug("key %d", key_serial(key));
|
|
|
|
|
|
|
|
data.kver = 1;
|
2009-09-14 01:17:35 +00:00
|
|
|
data.v1.security_index = RXRPC_SECURITY_RXKAD;
|
|
|
|
data.v1.ticket_length = 0;
|
2017-08-29 09:15:40 +00:00
|
|
|
data.v1.expiry = rxrpc_time64_to_u32(expiry);
|
2009-09-14 01:17:35 +00:00
|
|
|
data.v1.kvno = 0;
|
2007-04-26 22:48:28 +00:00
|
|
|
|
2009-09-14 01:17:35 +00:00
|
|
|
memcpy(&data.v1.session_key, session_key, sizeof(data.v1.session_key));
|
2007-04-26 22:48:28 +00:00
|
|
|
|
|
|
|
ret = key_instantiate_and_link(key, &data, sizeof(data), NULL, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
2022-10-19 12:49:02 +00:00
|
|
|
conn->key = key;
|
2007-04-26 22:48:28 +00:00
|
|
|
_leave(" = 0 [%d]", key_serial(key));
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
key_revoke(key);
|
|
|
|
key_put(key);
|
|
|
|
_leave(" = -ENOMEM [ins %d]", ret);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(rxrpc_get_server_data_key);
|
2007-10-17 06:29:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* rxrpc_get_null_key - Generate a null RxRPC key
|
|
|
|
* @keyname: The name to give the key.
|
|
|
|
*
|
|
|
|
* Generate a null RxRPC key that can be used to indicate anonymous security is
|
|
|
|
* required for a particular domain.
|
|
|
|
*/
|
|
|
|
struct key *rxrpc_get_null_key(const char *keyname)
|
|
|
|
{
|
CRED: Inaugurate COW credentials
Inaugurate copy-on-write credentials management. This uses RCU to manage the
credentials pointer in the task_struct with respect to accesses by other tasks.
A process may only modify its own credentials, and so does not need locking to
access or modify its own credentials.
A mutex (cred_replace_mutex) is added to the task_struct to control the effect
of PTRACE_ATTACHED on credential calculations, particularly with respect to
execve().
With this patch, the contents of an active credentials struct may not be
changed directly; rather a new set of credentials must be prepared, modified
and committed using something like the following sequence of events:
struct cred *new = prepare_creds();
int ret = blah(new);
if (ret < 0) {
abort_creds(new);
return ret;
}
return commit_creds(new);
There are some exceptions to this rule: the keyrings pointed to by the active
credentials may be instantiated - keyrings violate the COW rule as managing
COW keyrings is tricky, given that it is possible for a task to directly alter
the keys in a keyring in use by another task.
To help enforce this, various pointers to sets of credentials, such as those in
the task_struct, are declared const. The purpose of this is compile-time
discouragement of altering credentials through those pointers. Once a set of
credentials has been made public through one of these pointers, it may not be
modified, except under special circumstances:
(1) Its reference count may incremented and decremented.
(2) The keyrings to which it points may be modified, but not replaced.
The only safe way to modify anything else is to create a replacement and commit
using the functions described in Documentation/credentials.txt (which will be
added by a later patch).
This patch and the preceding patches have been tested with the LTP SELinux
testsuite.
This patch makes several logical sets of alteration:
(1) execve().
This now prepares and commits credentials in various places in the
security code rather than altering the current creds directly.
(2) Temporary credential overrides.
do_coredump() and sys_faccessat() now prepare their own credentials and
temporarily override the ones currently on the acting thread, whilst
preventing interference from other threads by holding cred_replace_mutex
on the thread being dumped.
This will be replaced in a future patch by something that hands down the
credentials directly to the functions being called, rather than altering
the task's objective credentials.
(3) LSM interface.
A number of functions have been changed, added or removed:
(*) security_capset_check(), ->capset_check()
(*) security_capset_set(), ->capset_set()
Removed in favour of security_capset().
(*) security_capset(), ->capset()
New. This is passed a pointer to the new creds, a pointer to the old
creds and the proposed capability sets. It should fill in the new
creds or return an error. All pointers, barring the pointer to the
new creds, are now const.
(*) security_bprm_apply_creds(), ->bprm_apply_creds()
Changed; now returns a value, which will cause the process to be
killed if it's an error.
(*) security_task_alloc(), ->task_alloc_security()
Removed in favour of security_prepare_creds().
(*) security_cred_free(), ->cred_free()
New. Free security data attached to cred->security.
(*) security_prepare_creds(), ->cred_prepare()
New. Duplicate any security data attached to cred->security.
(*) security_commit_creds(), ->cred_commit()
New. Apply any security effects for the upcoming installation of new
security by commit_creds().
(*) security_task_post_setuid(), ->task_post_setuid()
Removed in favour of security_task_fix_setuid().
(*) security_task_fix_setuid(), ->task_fix_setuid()
Fix up the proposed new credentials for setuid(). This is used by
cap_set_fix_setuid() to implicitly adjust capabilities in line with
setuid() changes. Changes are made to the new credentials, rather
than the task itself as in security_task_post_setuid().
(*) security_task_reparent_to_init(), ->task_reparent_to_init()
Removed. Instead the task being reparented to init is referred
directly to init's credentials.
NOTE! This results in the loss of some state: SELinux's osid no
longer records the sid of the thread that forked it.
(*) security_key_alloc(), ->key_alloc()
(*) security_key_permission(), ->key_permission()
Changed. These now take cred pointers rather than task pointers to
refer to the security context.
(4) sys_capset().
This has been simplified and uses less locking. The LSM functions it
calls have been merged.
(5) reparent_to_kthreadd().
This gives the current thread the same credentials as init by simply using
commit_thread() to point that way.
(6) __sigqueue_alloc() and switch_uid()
__sigqueue_alloc() can't stop the target task from changing its creds
beneath it, so this function gets a reference to the currently applicable
user_struct which it then passes into the sigqueue struct it returns if
successful.
switch_uid() is now called from commit_creds(), and possibly should be
folded into that. commit_creds() should take care of protecting
__sigqueue_alloc().
(7) [sg]et[ug]id() and co and [sg]et_current_groups.
The set functions now all use prepare_creds(), commit_creds() and
abort_creds() to build and check a new set of credentials before applying
it.
security_task_set[ug]id() is called inside the prepared section. This
guarantees that nothing else will affect the creds until we've finished.
The calling of set_dumpable() has been moved into commit_creds().
Much of the functionality of set_user() has been moved into
commit_creds().
The get functions all simply access the data directly.
(8) security_task_prctl() and cap_task_prctl().
security_task_prctl() has been modified to return -ENOSYS if it doesn't
want to handle a function, or otherwise return the return value directly
rather than through an argument.
Additionally, cap_task_prctl() now prepares a new set of credentials, even
if it doesn't end up using it.
(9) Keyrings.
A number of changes have been made to the keyrings code:
(a) switch_uid_keyring(), copy_keys(), exit_keys() and suid_keys() have
all been dropped and built in to the credentials functions directly.
They may want separating out again later.
(b) key_alloc() and search_process_keyrings() now take a cred pointer
rather than a task pointer to specify the security context.
(c) copy_creds() gives a new thread within the same thread group a new
thread keyring if its parent had one, otherwise it discards the thread
keyring.
(d) The authorisation key now points directly to the credentials to extend
the search into rather pointing to the task that carries them.
(e) Installing thread, process or session keyrings causes a new set of
credentials to be created, even though it's not strictly necessary for
process or session keyrings (they're shared).
(10) Usermode helper.
The usermode helper code now carries a cred struct pointer in its
subprocess_info struct instead of a new session keyring pointer. This set
of credentials is derived from init_cred and installed on the new process
after it has been cloned.
call_usermodehelper_setup() allocates the new credentials and
call_usermodehelper_freeinfo() discards them if they haven't been used. A
special cred function (prepare_usermodeinfo_creds()) is provided
specifically for call_usermodehelper_setup() to call.
call_usermodehelper_setkeys() adjusts the credentials to sport the
supplied keyring as the new session keyring.
(11) SELinux.
SELinux has a number of changes, in addition to those to support the LSM
interface changes mentioned above:
(a) selinux_setprocattr() no longer does its check for whether the
current ptracer can access processes with the new SID inside the lock
that covers getting the ptracer's SID. Whilst this lock ensures that
the check is done with the ptracer pinned, the result is only valid
until the lock is released, so there's no point doing it inside the
lock.
(12) is_single_threaded().
This function has been extracted from selinux_setprocattr() and put into
a file of its own in the lib/ directory as join_session_keyring() now
wants to use it too.
The code in SELinux just checked to see whether a task shared mm_structs
with other tasks (CLONE_VM), but that isn't good enough. We really want
to know if they're part of the same thread group (CLONE_THREAD).
(13) nfsd.
The NFS server daemon now has to use the COW credentials to set the
credentials it is going to use. It really needs to pass the credentials
down to the functions it calls, but it can't do that until other patches
in this series have been applied.
Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: James Morris <jmorris@namei.org>
Signed-off-by: James Morris <jmorris@namei.org>
2008-11-13 23:39:23 +00:00
|
|
|
const struct cred *cred = current_cred();
|
2007-10-17 06:29:46 +00:00
|
|
|
struct key *key;
|
|
|
|
int ret;
|
|
|
|
|
2012-05-25 22:37:54 +00:00
|
|
|
key = key_alloc(&key_type_rxrpc, keyname,
|
|
|
|
GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
|
2019-07-11 01:43:43 +00:00
|
|
|
KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA, NULL);
|
2007-10-17 06:29:46 +00:00
|
|
|
if (IS_ERR(key))
|
|
|
|
return key;
|
|
|
|
|
|
|
|
ret = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
|
|
|
|
if (ret < 0) {
|
|
|
|
key_revoke(key);
|
|
|
|
key_put(key);
|
|
|
|
return ERR_PTR(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(rxrpc_get_null_key);
|
2009-09-14 01:17:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* read the contents of an rxrpc key
|
|
|
|
* - this returns the result in XDR form
|
|
|
|
*/
|
|
|
|
static long rxrpc_read(const struct key *key,
|
2020-03-22 01:11:24 +00:00
|
|
|
char *buffer, size_t buflen)
|
2009-09-14 01:17:40 +00:00
|
|
|
{
|
2009-09-14 01:17:46 +00:00
|
|
|
const struct rxrpc_key_token *token;
|
|
|
|
size_t size;
|
2020-03-22 01:11:24 +00:00
|
|
|
__be32 *xdr, *oldxdr;
|
2009-09-14 01:17:46 +00:00
|
|
|
u32 cnlen, toksize, ntoks, tok, zero;
|
|
|
|
u16 toksizes[AFSTOKEN_MAX];
|
2009-09-14 01:17:40 +00:00
|
|
|
|
|
|
|
_enter("");
|
|
|
|
|
|
|
|
/* we don't know what form we should return non-AFS keys in */
|
|
|
|
if (memcmp(key->description, "afs@", 4) != 0)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
cnlen = strlen(key->description + 4);
|
|
|
|
|
2009-09-14 01:17:46 +00:00
|
|
|
#define RND(X) (((X) + 3) & ~3)
|
|
|
|
|
2009-09-14 01:17:40 +00:00
|
|
|
/* AFS keys we return in XDR form, so we need to work out the size of
|
|
|
|
* the XDR */
|
|
|
|
size = 2 * 4; /* flags, cellname len */
|
2009-09-14 01:17:46 +00:00
|
|
|
size += RND(cnlen); /* cellname */
|
2009-09-14 01:17:40 +00:00
|
|
|
size += 1 * 4; /* token count */
|
|
|
|
|
|
|
|
ntoks = 0;
|
2015-10-21 13:04:48 +00:00
|
|
|
for (token = key->payload.data[0]; token; token = token->next) {
|
2009-09-14 01:17:46 +00:00
|
|
|
toksize = 4; /* sec index */
|
|
|
|
|
2009-09-14 01:17:40 +00:00
|
|
|
switch (token->security_index) {
|
|
|
|
case RXRPC_SECURITY_RXKAD:
|
2020-09-04 17:01:24 +00:00
|
|
|
toksize += 8 * 4; /* viceid, kvno, key*2, begin,
|
2009-09-14 01:17:46 +00:00
|
|
|
* end, primary, tktlen */
|
2020-09-27 10:07:21 +00:00
|
|
|
if (!token->no_leak_key)
|
|
|
|
toksize += RND(token->kad->ticket_len);
|
2009-09-14 01:17:40 +00:00
|
|
|
break;
|
|
|
|
|
2009-09-14 01:17:46 +00:00
|
|
|
default: /* we have a ticket we can't encode */
|
2020-09-08 21:09:04 +00:00
|
|
|
pr_err("Unsupported key token type (%u)\n",
|
|
|
|
token->security_index);
|
2021-01-12 15:23:51 +00:00
|
|
|
return -ENOPKG;
|
2009-09-14 01:17:40 +00:00
|
|
|
}
|
2009-09-14 01:17:46 +00:00
|
|
|
|
|
|
|
_debug("token[%u]: toksize=%u", ntoks, toksize);
|
2022-12-01 12:24:36 +00:00
|
|
|
if (WARN_ON(toksize > AFSTOKEN_LENGTH_MAX))
|
|
|
|
return -EIO;
|
2009-09-14 01:17:46 +00:00
|
|
|
|
|
|
|
toksizes[ntoks++] = toksize;
|
|
|
|
size += toksize + 4; /* each token has a length word */
|
2009-09-14 01:17:40 +00:00
|
|
|
}
|
|
|
|
|
2009-09-14 01:17:46 +00:00
|
|
|
#undef RND
|
|
|
|
|
2009-09-14 01:17:40 +00:00
|
|
|
if (!buffer || buflen < size)
|
|
|
|
return size;
|
|
|
|
|
2020-03-22 01:11:24 +00:00
|
|
|
xdr = (__be32 *)buffer;
|
2009-09-14 01:17:40 +00:00
|
|
|
zero = 0;
|
|
|
|
#define ENCODE(x) \
|
|
|
|
do { \
|
2020-03-22 01:11:24 +00:00
|
|
|
*xdr++ = htonl(x); \
|
2009-09-14 01:17:40 +00:00
|
|
|
} while(0)
|
2009-09-14 01:17:46 +00:00
|
|
|
#define ENCODE_DATA(l, s) \
|
|
|
|
do { \
|
|
|
|
u32 _l = (l); \
|
|
|
|
ENCODE(l); \
|
2020-03-22 01:11:24 +00:00
|
|
|
memcpy(xdr, (s), _l); \
|
|
|
|
if (_l & 3) \
|
|
|
|
memcpy((u8 *)xdr + _l, &zero, 4 - (_l & 3)); \
|
2009-09-14 01:17:46 +00:00
|
|
|
xdr += (_l + 3) >> 2; \
|
|
|
|
} while(0)
|
2020-09-04 17:01:24 +00:00
|
|
|
#define ENCODE_BYTES(l, s) \
|
|
|
|
do { \
|
|
|
|
u32 _l = (l); \
|
|
|
|
memcpy(xdr, (s), _l); \
|
|
|
|
if (_l & 3) \
|
|
|
|
memcpy((u8 *)xdr + _l, &zero, 4 - (_l & 3)); \
|
|
|
|
xdr += (_l + 3) >> 2; \
|
|
|
|
} while(0)
|
2009-09-14 01:17:46 +00:00
|
|
|
#define ENCODE64(x) \
|
|
|
|
do { \
|
|
|
|
__be64 y = cpu_to_be64(x); \
|
2020-03-22 01:11:24 +00:00
|
|
|
memcpy(xdr, &y, 8); \
|
2009-09-14 01:17:46 +00:00
|
|
|
xdr += 8 >> 2; \
|
|
|
|
} while(0)
|
|
|
|
#define ENCODE_STR(s) \
|
|
|
|
do { \
|
|
|
|
const char *_s = (s); \
|
|
|
|
ENCODE_DATA(strlen(_s), _s); \
|
|
|
|
} while(0)
|
2009-09-14 01:17:40 +00:00
|
|
|
|
2009-09-14 01:17:46 +00:00
|
|
|
ENCODE(0); /* flags */
|
|
|
|
ENCODE_DATA(cnlen, key->description + 4); /* cellname */
|
|
|
|
ENCODE(ntoks);
|
2009-09-14 01:17:40 +00:00
|
|
|
|
2009-09-14 01:17:46 +00:00
|
|
|
tok = 0;
|
2015-10-21 13:04:48 +00:00
|
|
|
for (token = key->payload.data[0]; token; token = token->next) {
|
2009-09-14 01:17:46 +00:00
|
|
|
toksize = toksizes[tok++];
|
|
|
|
ENCODE(toksize);
|
|
|
|
oldxdr = xdr;
|
|
|
|
ENCODE(token->security_index);
|
2009-09-14 01:17:40 +00:00
|
|
|
|
|
|
|
switch (token->security_index) {
|
|
|
|
case RXRPC_SECURITY_RXKAD:
|
|
|
|
ENCODE(token->kad->vice_id);
|
|
|
|
ENCODE(token->kad->kvno);
|
2020-09-04 17:01:24 +00:00
|
|
|
ENCODE_BYTES(8, token->kad->session_key);
|
2009-09-14 01:17:40 +00:00
|
|
|
ENCODE(token->kad->start);
|
|
|
|
ENCODE(token->kad->expiry);
|
|
|
|
ENCODE(token->kad->primary_flag);
|
2020-09-27 10:07:21 +00:00
|
|
|
if (token->no_leak_key)
|
|
|
|
ENCODE(0);
|
|
|
|
else
|
|
|
|
ENCODE_DATA(token->kad->ticket_len, token->kad->ticket);
|
2009-09-14 01:17:46 +00:00
|
|
|
break;
|
|
|
|
|
2009-09-14 01:17:40 +00:00
|
|
|
default:
|
2021-01-12 15:23:51 +00:00
|
|
|
pr_err("Unsupported key token type (%u)\n",
|
|
|
|
token->security_index);
|
|
|
|
return -ENOPKG;
|
2009-09-14 01:17:40 +00:00
|
|
|
}
|
2009-09-14 01:17:46 +00:00
|
|
|
|
2023-04-21 16:16:17 +00:00
|
|
|
if (WARN_ON((unsigned long)xdr - (unsigned long)oldxdr !=
|
2022-12-01 12:24:36 +00:00
|
|
|
toksize))
|
|
|
|
return -EIO;
|
2009-09-14 01:17:40 +00:00
|
|
|
}
|
|
|
|
|
2009-09-14 01:17:46 +00:00
|
|
|
#undef ENCODE_STR
|
|
|
|
#undef ENCODE_DATA
|
|
|
|
#undef ENCODE64
|
2009-09-14 01:17:40 +00:00
|
|
|
#undef ENCODE
|
|
|
|
|
2022-12-01 12:24:36 +00:00
|
|
|
if (WARN_ON(tok != ntoks))
|
|
|
|
return -EIO;
|
|
|
|
if (WARN_ON((unsigned long)xdr - (unsigned long)buffer != size))
|
|
|
|
return -EIO;
|
2009-09-14 01:17:40 +00:00
|
|
|
_leave(" = %zu", size);
|
|
|
|
return size;
|
|
|
|
}
|