2019-05-27 14:55:01 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2007-04-27 06:48:28 +08: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:
|
|
|
|
* "afs@CAMBRIDGE.REDHAT.COM>
|
|
|
|
*/
|
|
|
|
|
2016-06-03 03:08:52 +08:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2016-01-24 21:19:01 +08:00
|
|
|
#include <crypto/skcipher.h>
|
2007-04-27 06:48:28 +08:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/net.h>
|
|
|
|
#include <linux/skbuff.h>
|
2007-10-17 14:29:46 +08:00
|
|
|
#include <linux/key-type.h>
|
2009-09-14 09:17:35 +08: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 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2007-04-27 06:48:28 +08:00
|
|
|
#include <net/sock.h>
|
|
|
|
#include <net/af_rxrpc.h>
|
|
|
|
#include <keys/rxrpc-type.h>
|
|
|
|
#include <keys/user-type.h>
|
|
|
|
#include "ar-internal.h"
|
|
|
|
|
2011-03-07 23:05:59 +08:00
|
|
|
static int rxrpc_vet_description_s(const char *);
|
2014-07-19 01:56:36 +08:00
|
|
|
static int rxrpc_preparse(struct key_preparsed_payload *);
|
|
|
|
static int rxrpc_preparse_s(struct key_preparsed_payload *);
|
|
|
|
static void rxrpc_free_preparse(struct key_preparsed_payload *);
|
|
|
|
static void rxrpc_free_preparse_s(struct key_preparsed_payload *);
|
2007-04-27 06:48:28 +08:00
|
|
|
static void rxrpc_destroy(struct key *);
|
|
|
|
static void rxrpc_destroy_s(struct key *);
|
|
|
|
static void rxrpc_describe(const struct key *, struct seq_file *);
|
2020-03-22 09:11:24 +08:00
|
|
|
static long rxrpc_read(const struct key *, char *, size_t);
|
2007-04-27 06:48:28 +08: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-27 04:02:33 +08:00
|
|
|
.flags = KEY_TYPE_NET_DOMAIN,
|
2014-07-19 01:56:36 +08:00
|
|
|
.preparse = rxrpc_preparse,
|
|
|
|
.free_preparse = rxrpc_free_preparse,
|
|
|
|
.instantiate = generic_key_instantiate,
|
2007-04-27 06:48:28 +08:00
|
|
|
.destroy = rxrpc_destroy,
|
|
|
|
.describe = rxrpc_describe,
|
2009-09-14 09:17:40 +08:00
|
|
|
.read = rxrpc_read,
|
2007-04-27 06:48:28 +08:00
|
|
|
};
|
|
|
|
EXPORT_SYMBOL(key_type_rxrpc);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rxrpc server defined keys take "<serviceId>:<securityIndex>" as the
|
|
|
|
* description and an 8-byte decryption key as the payload
|
|
|
|
*/
|
|
|
|
struct key_type key_type_rxrpc_s = {
|
|
|
|
.name = "rxrpc_s",
|
2019-06-27 04:02:33 +08:00
|
|
|
.flags = KEY_TYPE_NET_DOMAIN,
|
2011-03-07 23:05:59 +08:00
|
|
|
.vet_description = rxrpc_vet_description_s,
|
2014-07-19 01:56:36 +08:00
|
|
|
.preparse = rxrpc_preparse_s,
|
|
|
|
.free_preparse = rxrpc_free_preparse_s,
|
|
|
|
.instantiate = generic_key_instantiate,
|
2007-04-27 06:48:28 +08:00
|
|
|
.destroy = rxrpc_destroy_s,
|
|
|
|
.describe = rxrpc_describe,
|
|
|
|
};
|
|
|
|
|
2011-03-07 23:05:59 +08:00
|
|
|
/*
|
|
|
|
* Vet the description for an RxRPC server key
|
|
|
|
*/
|
|
|
|
static int rxrpc_vet_description_s(const char *desc)
|
|
|
|
{
|
|
|
|
unsigned long num;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
num = simple_strtoul(desc, &p, 10);
|
|
|
|
if (*p != ':' || num > 65535)
|
|
|
|
return -EINVAL;
|
|
|
|
num = simple_strtoul(p + 1, &p, 10);
|
|
|
|
if (*p || num < 1 || num > 255)
|
|
|
|
return -EINVAL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-09-14 09:17:35 +08:00
|
|
|
/*
|
|
|
|
* parse an RxKAD type XDR format token
|
|
|
|
* - the caller guarantees we have at least 4 words
|
|
|
|
*/
|
2014-07-19 01:56:36 +08:00
|
|
|
static int rxrpc_preparse_xdr_rxkad(struct key_preparsed_payload *prep,
|
|
|
|
size_t datalen,
|
|
|
|
const __be32 *xdr, unsigned int toklen)
|
2009-09-14 09:17:35 +08:00
|
|
|
{
|
2009-09-14 09:17:46 +08:00
|
|
|
struct rxrpc_key_token *token, **pptoken;
|
2017-08-29 17:15:40 +08:00
|
|
|
time64_t expiry;
|
2009-09-14 09:17:35 +08: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 22:51:22 +08:00
|
|
|
if (toklen < 8 * 4 + tktlen)
|
2009-09-14 09:17:35 +08:00
|
|
|
return -EKEYREJECTED;
|
|
|
|
|
|
|
|
plen = sizeof(*token) + sizeof(*token->kad) + tktlen;
|
2014-07-19 01:56:36 +08:00
|
|
|
prep->quotalen = datalen + plen;
|
2009-09-14 09:17:35 +08:00
|
|
|
|
|
|
|
plen -= sizeof(*token);
|
2011-02-25 23:33:17 +08:00
|
|
|
token = kzalloc(sizeof(*token), GFP_KERNEL);
|
2009-09-14 09:17:35 +08:00
|
|
|
if (!token)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2011-02-25 23:33:17 +08:00
|
|
|
token->kad = kzalloc(plen, GFP_KERNEL);
|
2009-09-14 09:17:35 +08: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 21:04:48 +08:00
|
|
|
prep->payload.data[1] = (void *)((unsigned long)prep->payload.data[1] + 1);
|
2009-09-14 09:17:35 +08:00
|
|
|
|
|
|
|
/* attach the data */
|
2015-10-21 21:04:48 +08:00
|
|
|
for (pptoken = (struct rxrpc_key_token **)&prep->payload.data[0];
|
2009-09-14 09:17:46 +08:00
|
|
|
*pptoken;
|
|
|
|
pptoken = &(*pptoken)->next)
|
|
|
|
continue;
|
|
|
|
*pptoken = token;
|
2017-08-29 17:15:40 +08:00
|
|
|
expiry = rxrpc_u32_to_time64(token->kad->expiry);
|
|
|
|
if (expiry < prep->expiry)
|
|
|
|
prep->expiry = expiry;
|
2009-09-14 09:17:46 +08:00
|
|
|
|
|
|
|
_leave(" = 0");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rxrpc_free_krb5_principal(struct krb5_principal *princ)
|
|
|
|
{
|
|
|
|
int loop;
|
|
|
|
|
|
|
|
if (princ->name_parts) {
|
|
|
|
for (loop = princ->n_name_parts - 1; loop >= 0; loop--)
|
|
|
|
kfree(princ->name_parts[loop]);
|
|
|
|
kfree(princ->name_parts);
|
|
|
|
}
|
|
|
|
kfree(princ->realm);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rxrpc_free_krb5_tagged(struct krb5_tagged_data *td)
|
|
|
|
{
|
|
|
|
kfree(td->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* free up an RxK5 token
|
|
|
|
*/
|
|
|
|
static void rxrpc_rxk5_free(struct rxk5_key *rxk5)
|
|
|
|
{
|
|
|
|
int loop;
|
|
|
|
|
|
|
|
rxrpc_free_krb5_principal(&rxk5->client);
|
|
|
|
rxrpc_free_krb5_principal(&rxk5->server);
|
|
|
|
rxrpc_free_krb5_tagged(&rxk5->session);
|
|
|
|
|
|
|
|
if (rxk5->addresses) {
|
|
|
|
for (loop = rxk5->n_addresses - 1; loop >= 0; loop--)
|
|
|
|
rxrpc_free_krb5_tagged(&rxk5->addresses[loop]);
|
|
|
|
kfree(rxk5->addresses);
|
|
|
|
}
|
|
|
|
if (rxk5->authdata) {
|
|
|
|
for (loop = rxk5->n_authdata - 1; loop >= 0; loop--)
|
|
|
|
rxrpc_free_krb5_tagged(&rxk5->authdata[loop]);
|
|
|
|
kfree(rxk5->authdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
kfree(rxk5->ticket);
|
|
|
|
kfree(rxk5->ticket2);
|
|
|
|
kfree(rxk5);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* extract a krb5 principal
|
|
|
|
*/
|
|
|
|
static int rxrpc_krb5_decode_principal(struct krb5_principal *princ,
|
|
|
|
const __be32 **_xdr,
|
2012-04-15 13:58:06 +08:00
|
|
|
unsigned int *_toklen)
|
2009-09-14 09:17:46 +08:00
|
|
|
{
|
|
|
|
const __be32 *xdr = *_xdr;
|
2017-06-15 07:12:24 +08:00
|
|
|
unsigned int toklen = *_toklen, n_parts, loop, tmp, paddedlen;
|
2009-09-14 09:17:46 +08:00
|
|
|
|
|
|
|
/* there must be at least one name, and at least #names+1 length
|
|
|
|
* words */
|
|
|
|
if (toklen <= 12)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
_enter(",{%x,%x,%x},%u",
|
|
|
|
ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), toklen);
|
|
|
|
|
|
|
|
n_parts = ntohl(*xdr++);
|
|
|
|
toklen -= 4;
|
|
|
|
if (n_parts <= 0 || n_parts > AFSTOKEN_K5_COMPONENTS_MAX)
|
|
|
|
return -EINVAL;
|
|
|
|
princ->n_name_parts = n_parts;
|
|
|
|
|
|
|
|
if (toklen <= (n_parts + 1) * 4)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2012-02-14 04:19:14 +08:00
|
|
|
princ->name_parts = kcalloc(n_parts, sizeof(char *), GFP_KERNEL);
|
2009-09-14 09:17:46 +08:00
|
|
|
if (!princ->name_parts)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (loop = 0; loop < n_parts; loop++) {
|
|
|
|
if (toklen < 4)
|
|
|
|
return -EINVAL;
|
|
|
|
tmp = ntohl(*xdr++);
|
|
|
|
toklen -= 4;
|
|
|
|
if (tmp <= 0 || tmp > AFSTOKEN_STRING_MAX)
|
|
|
|
return -EINVAL;
|
2017-06-15 07:12:24 +08:00
|
|
|
paddedlen = (tmp + 3) & ~3;
|
|
|
|
if (paddedlen > toklen)
|
2009-09-14 09:17:46 +08:00
|
|
|
return -EINVAL;
|
|
|
|
princ->name_parts[loop] = kmalloc(tmp + 1, GFP_KERNEL);
|
|
|
|
if (!princ->name_parts[loop])
|
|
|
|
return -ENOMEM;
|
|
|
|
memcpy(princ->name_parts[loop], xdr, tmp);
|
|
|
|
princ->name_parts[loop][tmp] = 0;
|
2017-06-15 07:12:24 +08:00
|
|
|
toklen -= paddedlen;
|
|
|
|
xdr += paddedlen >> 2;
|
2009-09-14 09:17:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (toklen < 4)
|
|
|
|
return -EINVAL;
|
|
|
|
tmp = ntohl(*xdr++);
|
|
|
|
toklen -= 4;
|
|
|
|
if (tmp <= 0 || tmp > AFSTOKEN_K5_REALM_MAX)
|
|
|
|
return -EINVAL;
|
2017-06-15 07:12:24 +08:00
|
|
|
paddedlen = (tmp + 3) & ~3;
|
|
|
|
if (paddedlen > toklen)
|
2009-09-14 09:17:46 +08:00
|
|
|
return -EINVAL;
|
|
|
|
princ->realm = kmalloc(tmp + 1, GFP_KERNEL);
|
|
|
|
if (!princ->realm)
|
|
|
|
return -ENOMEM;
|
|
|
|
memcpy(princ->realm, xdr, tmp);
|
|
|
|
princ->realm[tmp] = 0;
|
2017-06-15 07:12:24 +08:00
|
|
|
toklen -= paddedlen;
|
|
|
|
xdr += paddedlen >> 2;
|
2009-09-14 09:17:46 +08:00
|
|
|
|
|
|
|
_debug("%s/...@%s", princ->name_parts[0], princ->realm);
|
|
|
|
|
|
|
|
*_xdr = xdr;
|
|
|
|
*_toklen = toklen;
|
|
|
|
_leave(" = 0 [toklen=%u]", toklen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* extract a piece of krb5 tagged data
|
|
|
|
*/
|
|
|
|
static int rxrpc_krb5_decode_tagged_data(struct krb5_tagged_data *td,
|
|
|
|
size_t max_data_size,
|
|
|
|
const __be32 **_xdr,
|
2012-04-15 13:58:06 +08:00
|
|
|
unsigned int *_toklen)
|
2009-09-14 09:17:46 +08:00
|
|
|
{
|
|
|
|
const __be32 *xdr = *_xdr;
|
2017-06-15 07:12:24 +08:00
|
|
|
unsigned int toklen = *_toklen, len, paddedlen;
|
2009-09-14 09:17:46 +08:00
|
|
|
|
|
|
|
/* there must be at least one tag and one length word */
|
|
|
|
if (toklen <= 8)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
_enter(",%zu,{%x,%x},%u",
|
|
|
|
max_data_size, ntohl(xdr[0]), ntohl(xdr[1]), toklen);
|
|
|
|
|
|
|
|
td->tag = ntohl(*xdr++);
|
|
|
|
len = ntohl(*xdr++);
|
|
|
|
toklen -= 8;
|
|
|
|
if (len > max_data_size)
|
|
|
|
return -EINVAL;
|
2017-06-15 07:12:24 +08:00
|
|
|
paddedlen = (len + 3) & ~3;
|
|
|
|
if (paddedlen > toklen)
|
|
|
|
return -EINVAL;
|
2009-09-14 09:17:46 +08:00
|
|
|
td->data_len = len;
|
|
|
|
|
|
|
|
if (len > 0) {
|
2011-11-17 20:43:40 +08:00
|
|
|
td->data = kmemdup(xdr, len, GFP_KERNEL);
|
2009-09-14 09:17:46 +08:00
|
|
|
if (!td->data)
|
|
|
|
return -ENOMEM;
|
2017-06-15 07:12:24 +08:00
|
|
|
toklen -= paddedlen;
|
|
|
|
xdr += paddedlen >> 2;
|
2009-09-14 09:17:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_debug("tag %x len %x", td->tag, td->data_len);
|
|
|
|
|
|
|
|
*_xdr = xdr;
|
|
|
|
*_toklen = toklen;
|
|
|
|
_leave(" = 0 [toklen=%u]", toklen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* extract an array of tagged data
|
|
|
|
*/
|
|
|
|
static int rxrpc_krb5_decode_tagged_array(struct krb5_tagged_data **_td,
|
|
|
|
u8 *_n_elem,
|
|
|
|
u8 max_n_elem,
|
|
|
|
size_t max_elem_size,
|
|
|
|
const __be32 **_xdr,
|
2012-04-15 13:58:06 +08:00
|
|
|
unsigned int *_toklen)
|
2009-09-14 09:17:46 +08:00
|
|
|
{
|
|
|
|
struct krb5_tagged_data *td;
|
|
|
|
const __be32 *xdr = *_xdr;
|
2012-04-15 13:58:06 +08:00
|
|
|
unsigned int toklen = *_toklen, n_elem, loop;
|
2009-09-14 09:17:46 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* there must be at least one count */
|
|
|
|
if (toklen < 4)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
_enter(",,%u,%zu,{%x},%u",
|
|
|
|
max_n_elem, max_elem_size, ntohl(xdr[0]), toklen);
|
|
|
|
|
|
|
|
n_elem = ntohl(*xdr++);
|
|
|
|
toklen -= 4;
|
2014-07-18 23:31:57 +08:00
|
|
|
if (n_elem > max_n_elem)
|
2009-09-14 09:17:46 +08:00
|
|
|
return -EINVAL;
|
|
|
|
*_n_elem = n_elem;
|
|
|
|
if (n_elem > 0) {
|
|
|
|
if (toklen <= (n_elem + 1) * 4)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
_debug("n_elem %d", n_elem);
|
|
|
|
|
2012-02-14 04:19:14 +08:00
|
|
|
td = kcalloc(n_elem, sizeof(struct krb5_tagged_data),
|
2009-09-14 09:17:46 +08:00
|
|
|
GFP_KERNEL);
|
|
|
|
if (!td)
|
|
|
|
return -ENOMEM;
|
|
|
|
*_td = td;
|
|
|
|
|
|
|
|
for (loop = 0; loop < n_elem; loop++) {
|
|
|
|
ret = rxrpc_krb5_decode_tagged_data(&td[loop],
|
|
|
|
max_elem_size,
|
|
|
|
&xdr, &toklen);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*_xdr = xdr;
|
|
|
|
*_toklen = toklen;
|
|
|
|
_leave(" = 0 [toklen=%u]", toklen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* extract a krb5 ticket
|
|
|
|
*/
|
2009-09-16 15:01:13 +08:00
|
|
|
static int rxrpc_krb5_decode_ticket(u8 **_ticket, u16 *_tktlen,
|
2012-04-15 13:58:06 +08:00
|
|
|
const __be32 **_xdr, unsigned int *_toklen)
|
2009-09-14 09:17:46 +08:00
|
|
|
{
|
|
|
|
const __be32 *xdr = *_xdr;
|
2017-06-15 07:12:24 +08:00
|
|
|
unsigned int toklen = *_toklen, len, paddedlen;
|
2009-09-14 09:17:46 +08:00
|
|
|
|
|
|
|
/* there must be at least one length word */
|
|
|
|
if (toklen <= 4)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
_enter(",{%x},%u", ntohl(xdr[0]), toklen);
|
|
|
|
|
|
|
|
len = ntohl(*xdr++);
|
|
|
|
toklen -= 4;
|
|
|
|
if (len > AFSTOKEN_K5_TIX_MAX)
|
|
|
|
return -EINVAL;
|
2017-06-15 07:12:24 +08:00
|
|
|
paddedlen = (len + 3) & ~3;
|
|
|
|
if (paddedlen > toklen)
|
|
|
|
return -EINVAL;
|
2009-09-14 09:17:46 +08:00
|
|
|
*_tktlen = len;
|
|
|
|
|
|
|
|
_debug("ticket len %u", len);
|
|
|
|
|
|
|
|
if (len > 0) {
|
2011-11-17 20:43:40 +08:00
|
|
|
*_ticket = kmemdup(xdr, len, GFP_KERNEL);
|
2009-09-14 09:17:46 +08:00
|
|
|
if (!*_ticket)
|
|
|
|
return -ENOMEM;
|
2017-06-15 07:12:24 +08:00
|
|
|
toklen -= paddedlen;
|
|
|
|
xdr += paddedlen >> 2;
|
2009-09-14 09:17:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*_xdr = xdr;
|
|
|
|
*_toklen = toklen;
|
|
|
|
_leave(" = 0 [toklen=%u]", toklen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parse an RxK5 type XDR format token
|
|
|
|
* - the caller guarantees we have at least 4 words
|
|
|
|
*/
|
2014-07-19 01:56:36 +08:00
|
|
|
static int rxrpc_preparse_xdr_rxk5(struct key_preparsed_payload *prep,
|
|
|
|
size_t datalen,
|
|
|
|
const __be32 *xdr, unsigned int toklen)
|
2009-09-14 09:17:46 +08:00
|
|
|
{
|
|
|
|
struct rxrpc_key_token *token, **pptoken;
|
|
|
|
struct rxk5_key *rxk5;
|
|
|
|
const __be32 *end_xdr = xdr + (toklen >> 2);
|
2017-08-29 17:15:40 +08:00
|
|
|
time64_t expiry;
|
2009-09-14 09:17:46 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
_enter(",{%x,%x,%x,%x},%u",
|
|
|
|
ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
|
|
|
|
toklen);
|
|
|
|
|
|
|
|
/* reserve some payload space for this subkey - the length of the token
|
|
|
|
* is a reasonable approximation */
|
2014-07-19 01:56:36 +08:00
|
|
|
prep->quotalen = datalen + toklen;
|
2009-09-14 09:17:46 +08:00
|
|
|
|
|
|
|
token = kzalloc(sizeof(*token), GFP_KERNEL);
|
|
|
|
if (!token)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
rxk5 = kzalloc(sizeof(*rxk5), GFP_KERNEL);
|
|
|
|
if (!rxk5) {
|
|
|
|
kfree(token);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
token->security_index = RXRPC_SECURITY_RXK5;
|
|
|
|
token->k5 = rxk5;
|
|
|
|
|
|
|
|
/* extract the principals */
|
|
|
|
ret = rxrpc_krb5_decode_principal(&rxk5->client, &xdr, &toklen);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
ret = rxrpc_krb5_decode_principal(&rxk5->server, &xdr, &toklen);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* extract the session key and the encoding type (the tag field ->
|
|
|
|
* ENCTYPE_xxx) */
|
|
|
|
ret = rxrpc_krb5_decode_tagged_data(&rxk5->session, AFSTOKEN_DATA_MAX,
|
|
|
|
&xdr, &toklen);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (toklen < 4 * 8 + 2 * 4)
|
|
|
|
goto inval;
|
|
|
|
rxk5->authtime = be64_to_cpup((const __be64 *) xdr);
|
|
|
|
xdr += 2;
|
|
|
|
rxk5->starttime = be64_to_cpup((const __be64 *) xdr);
|
|
|
|
xdr += 2;
|
|
|
|
rxk5->endtime = be64_to_cpup((const __be64 *) xdr);
|
|
|
|
xdr += 2;
|
|
|
|
rxk5->renew_till = be64_to_cpup((const __be64 *) xdr);
|
|
|
|
xdr += 2;
|
|
|
|
rxk5->is_skey = ntohl(*xdr++);
|
|
|
|
rxk5->flags = ntohl(*xdr++);
|
|
|
|
toklen -= 4 * 8 + 2 * 4;
|
|
|
|
|
|
|
|
_debug("times: a=%llx s=%llx e=%llx rt=%llx",
|
|
|
|
rxk5->authtime, rxk5->starttime, rxk5->endtime,
|
|
|
|
rxk5->renew_till);
|
|
|
|
_debug("is_skey=%x flags=%x", rxk5->is_skey, rxk5->flags);
|
|
|
|
|
|
|
|
/* extract the permitted client addresses */
|
|
|
|
ret = rxrpc_krb5_decode_tagged_array(&rxk5->addresses,
|
|
|
|
&rxk5->n_addresses,
|
|
|
|
AFSTOKEN_K5_ADDRESSES_MAX,
|
|
|
|
AFSTOKEN_DATA_MAX,
|
|
|
|
&xdr, &toklen);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
|
|
|
|
|
|
|
|
/* extract the tickets */
|
|
|
|
ret = rxrpc_krb5_decode_ticket(&rxk5->ticket, &rxk5->ticket_len,
|
|
|
|
&xdr, &toklen);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
ret = rxrpc_krb5_decode_ticket(&rxk5->ticket2, &rxk5->ticket2_len,
|
|
|
|
&xdr, &toklen);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
|
|
|
|
|
|
|
|
/* extract the typed auth data */
|
|
|
|
ret = rxrpc_krb5_decode_tagged_array(&rxk5->authdata,
|
|
|
|
&rxk5->n_authdata,
|
|
|
|
AFSTOKEN_K5_AUTHDATA_MAX,
|
|
|
|
AFSTOKEN_BDATALN_MAX,
|
|
|
|
&xdr, &toklen);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
|
|
|
|
|
|
|
|
if (toklen != 0)
|
|
|
|
goto inval;
|
|
|
|
|
2014-07-19 01:56:36 +08:00
|
|
|
/* attach the payload */
|
2015-10-21 21:04:48 +08:00
|
|
|
for (pptoken = (struct rxrpc_key_token **)&prep->payload.data[0];
|
2009-09-14 09:17:46 +08:00
|
|
|
*pptoken;
|
|
|
|
pptoken = &(*pptoken)->next)
|
|
|
|
continue;
|
|
|
|
*pptoken = token;
|
2017-08-29 17:15:40 +08:00
|
|
|
expiry = rxrpc_u32_to_time64(token->k5->endtime);
|
2017-08-29 17:15:40 +08:00
|
|
|
if (expiry < prep->expiry)
|
|
|
|
prep->expiry = expiry;
|
2009-09-14 09:17:35 +08:00
|
|
|
|
|
|
|
_leave(" = 0");
|
|
|
|
return 0;
|
2009-09-14 09:17:46 +08:00
|
|
|
|
|
|
|
inval:
|
|
|
|
ret = -EINVAL;
|
|
|
|
error:
|
|
|
|
rxrpc_rxk5_free(rxk5);
|
|
|
|
kfree(token);
|
|
|
|
_leave(" = %d", ret);
|
|
|
|
return ret;
|
2009-09-14 09:17:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* attempt to parse the data as the XDR format
|
|
|
|
* - the caller guarantees we have more than 7 words
|
|
|
|
*/
|
2014-07-19 01:56:36 +08:00
|
|
|
static int rxrpc_preparse_xdr(struct key_preparsed_payload *prep)
|
2009-09-14 09:17:35 +08:00
|
|
|
{
|
2014-07-19 01:56:36 +08:00
|
|
|
const __be32 *xdr = prep->data, *token;
|
2009-09-14 09:17:35 +08:00
|
|
|
const char *cp;
|
2017-06-15 07:12:24 +08:00
|
|
|
unsigned int len, paddedlen, loop, ntoken, toklen, sec_ix;
|
2014-07-19 01:56:36 +08:00
|
|
|
size_t datalen = prep->datalen;
|
2009-09-14 09:17:35 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
_enter(",{%x,%x,%x,%x},%zu",
|
|
|
|
ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
|
2014-07-19 01:56:36 +08:00
|
|
|
prep->datalen);
|
2009-09-14 09:17:35 +08: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-15 07:12:24 +08:00
|
|
|
paddedlen = (len + 3) & ~3;
|
|
|
|
if (paddedlen > datalen)
|
2009-09-14 09:17:35 +08:00
|
|
|
goto not_xdr;
|
|
|
|
|
|
|
|
cp = (const char *) xdr;
|
|
|
|
for (loop = 0; loop < len; loop++)
|
|
|
|
if (!isprint(cp[loop]))
|
|
|
|
goto not_xdr;
|
2017-06-15 07:12:24 +08:00
|
|
|
for (; loop < paddedlen; loop++)
|
|
|
|
if (cp[loop])
|
|
|
|
goto not_xdr;
|
2009-09-14 09:17:35 +08:00
|
|
|
_debug("cellname: [%u/%u] '%*.*s'",
|
2017-06-15 07:12:24 +08:00
|
|
|
len, paddedlen, len, len, (const char *) xdr);
|
|
|
|
datalen -= paddedlen;
|
|
|
|
xdr += paddedlen >> 2;
|
2009-09-14 09:17:35 +08: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 */
|
|
|
|
token = xdr;
|
|
|
|
loop = ntoken;
|
|
|
|
do {
|
|
|
|
if (datalen < 8)
|
|
|
|
goto not_xdr;
|
|
|
|
toklen = ntohl(*xdr++);
|
|
|
|
sec_ix = ntohl(*xdr);
|
|
|
|
datalen -= 4;
|
|
|
|
_debug("token: [%x/%zx] %x", toklen, datalen, sec_ix);
|
2017-06-15 07:12:24 +08:00
|
|
|
paddedlen = (toklen + 3) & ~3;
|
|
|
|
if (toklen < 20 || toklen > datalen || paddedlen > datalen)
|
2009-09-14 09:17:35 +08:00
|
|
|
goto not_xdr;
|
2017-06-15 07:12:24 +08:00
|
|
|
datalen -= paddedlen;
|
|
|
|
xdr += paddedlen >> 2;
|
2009-09-14 09:17:35 +08: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
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
xdr = token;
|
|
|
|
toklen = ntohl(*xdr++);
|
|
|
|
token = xdr + ((toklen + 3) >> 2);
|
|
|
|
sec_ix = ntohl(*xdr++);
|
|
|
|
toklen -= 4;
|
|
|
|
|
2009-09-14 09:17:46 +08:00
|
|
|
_debug("TOKEN type=%u [%p-%p]", sec_ix, xdr, token);
|
|
|
|
|
2009-09-14 09:17:35 +08:00
|
|
|
switch (sec_ix) {
|
|
|
|
case RXRPC_SECURITY_RXKAD:
|
2014-07-19 01:56:36 +08:00
|
|
|
ret = rxrpc_preparse_xdr_rxkad(prep, datalen, xdr, toklen);
|
2009-09-14 09:17:35 +08:00
|
|
|
if (ret != 0)
|
|
|
|
goto error;
|
|
|
|
break;
|
|
|
|
|
2009-09-14 09:17:46 +08:00
|
|
|
case RXRPC_SECURITY_RXK5:
|
2014-07-19 01:56:36 +08:00
|
|
|
ret = rxrpc_preparse_xdr_rxk5(prep, datalen, xdr, toklen);
|
2009-09-14 09:17:46 +08:00
|
|
|
if (ret != 0)
|
|
|
|
goto error;
|
|
|
|
break;
|
|
|
|
|
2009-09-14 09:17:35 +08:00
|
|
|
default:
|
|
|
|
ret = -EPROTONOSUPPORT;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (--ntoken > 0);
|
|
|
|
|
|
|
|
_leave(" = 0");
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
not_xdr:
|
|
|
|
_leave(" = -EPROTO");
|
|
|
|
return -EPROTO;
|
|
|
|
error:
|
|
|
|
_leave(" = %d", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-04-27 06:48:28 +08:00
|
|
|
/*
|
2014-07-19 01:56:36 +08:00
|
|
|
* Preparse an rxrpc defined key.
|
|
|
|
*
|
|
|
|
* Data should be of the form:
|
2007-04-27 06:48:28 +08: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-19 01:56:36 +08:00
|
|
|
static int rxrpc_preparse(struct key_preparsed_payload *prep)
|
2007-04-27 06:48:28 +08:00
|
|
|
{
|
2009-09-14 09:17:35 +08:00
|
|
|
const struct rxrpc_key_data_v1 *v1;
|
|
|
|
struct rxrpc_key_token *token, **pp;
|
2017-08-29 17:15:40 +08:00
|
|
|
time64_t expiry;
|
2007-04-27 06:48:28 +08:00
|
|
|
size_t plen;
|
|
|
|
u32 kver;
|
|
|
|
int ret;
|
|
|
|
|
2014-07-19 01:56:36 +08:00
|
|
|
_enter("%zu", prep->datalen);
|
2007-04-27 06:48:28 +08:00
|
|
|
|
|
|
|
/* handle a no-security key */
|
2012-09-13 20:06:29 +08:00
|
|
|
if (!prep->data && prep->datalen == 0)
|
2007-04-27 06:48:28 +08:00
|
|
|
return 0;
|
|
|
|
|
2009-09-14 09:17:35 +08:00
|
|
|
/* determine if the XDR payload format is being used */
|
2012-09-13 20:06:29 +08:00
|
|
|
if (prep->datalen > 7 * 4) {
|
2014-07-19 01:56:36 +08:00
|
|
|
ret = rxrpc_preparse_xdr(prep);
|
2009-09-14 09:17:35 +08:00
|
|
|
if (ret != -EPROTO)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-04-27 06:48:28 +08:00
|
|
|
/* get the key interface version number */
|
|
|
|
ret = -EINVAL;
|
2012-09-13 20:06:29 +08:00
|
|
|
if (prep->datalen <= 4 || !prep->data)
|
2007-04-27 06:48:28 +08:00
|
|
|
goto error;
|
2012-09-13 20:06:29 +08:00
|
|
|
memcpy(&kver, prep->data, sizeof(kver));
|
|
|
|
prep->data += sizeof(kver);
|
|
|
|
prep->datalen -= sizeof(kver);
|
2007-04-27 06:48:28 +08: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 20:06:29 +08:00
|
|
|
if (prep->datalen < sizeof(*v1))
|
2007-04-27 06:48:28 +08:00
|
|
|
goto error;
|
|
|
|
|
2012-09-13 20:06:29 +08:00
|
|
|
v1 = prep->data;
|
|
|
|
if (prep->datalen != sizeof(*v1) + v1->ticket_length)
|
2007-04-27 06:48:28 +08:00
|
|
|
goto error;
|
|
|
|
|
2009-09-14 09:17:35 +08: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-27 06:48:28 +08:00
|
|
|
_debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
|
2009-09-14 09:17:35 +08: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-27 06:48:28 +08:00
|
|
|
_debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
|
2009-09-14 09:17:35 +08: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-27 06:48:28 +08:00
|
|
|
|
|
|
|
ret = -EPROTONOSUPPORT;
|
2009-09-14 09:17:35 +08:00
|
|
|
if (v1->security_index != RXRPC_SECURITY_RXKAD)
|
2007-04-27 06:48:28 +08:00
|
|
|
goto error;
|
|
|
|
|
2009-09-14 09:17:35 +08:00
|
|
|
plen = sizeof(*token->kad) + v1->ticket_length;
|
2014-07-19 01:56:36 +08:00
|
|
|
prep->quotalen = plen + sizeof(*token);
|
2007-04-27 06:48:28 +08:00
|
|
|
|
|
|
|
ret = -ENOMEM;
|
2011-02-25 23:33:17 +08:00
|
|
|
token = kzalloc(sizeof(*token), GFP_KERNEL);
|
2009-09-14 09:17:35 +08:00
|
|
|
if (!token)
|
2007-04-27 06:48:28 +08:00
|
|
|
goto error;
|
2011-02-25 23:33:17 +08:00
|
|
|
token->kad = kzalloc(plen, GFP_KERNEL);
|
2009-09-14 09:17:35 +08: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-27 06:48:28 +08:00
|
|
|
|
2014-07-19 01:56:36 +08:00
|
|
|
/* count the number of tokens attached */
|
2015-10-21 21:04:48 +08:00
|
|
|
prep->payload.data[1] = (void *)((unsigned long)prep->payload.data[1] + 1);
|
2009-09-14 09:17:35 +08:00
|
|
|
|
2014-07-19 01:56:36 +08:00
|
|
|
/* attach the data */
|
2015-10-21 21:04:48 +08:00
|
|
|
pp = (struct rxrpc_key_token **)&prep->payload.data[0];
|
2009-09-14 09:17:35 +08:00
|
|
|
while (*pp)
|
|
|
|
pp = &(*pp)->next;
|
|
|
|
*pp = token;
|
2017-08-29 17:15:40 +08:00
|
|
|
expiry = rxrpc_u32_to_time64(token->kad->expiry);
|
|
|
|
if (expiry < prep->expiry)
|
|
|
|
prep->expiry = expiry;
|
2009-09-14 09:17:35 +08:00
|
|
|
token = NULL;
|
2007-04-27 06:48:28 +08:00
|
|
|
ret = 0;
|
|
|
|
|
2009-09-14 09:17:35 +08:00
|
|
|
error_free:
|
|
|
|
kfree(token);
|
2007-04-27 06:48:28 +08:00
|
|
|
error:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-07-19 01:56:36 +08:00
|
|
|
* Free token list.
|
2007-04-27 06:48:28 +08:00
|
|
|
*/
|
2014-07-19 01:56:36 +08: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;
|
|
|
|
case RXRPC_SECURITY_RXK5:
|
|
|
|
if (token->k5)
|
|
|
|
rxrpc_rxk5_free(token->k5);
|
|
|
|
break;
|
|
|
|
default:
|
2016-06-03 03:08:52 +08:00
|
|
|
pr_err("Unknown token type %x on rxrpc key\n",
|
2014-07-19 01:56:36 +08:00
|
|
|
token->security_index);
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
|
|
|
|
kfree(token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clean up preparse data.
|
|
|
|
*/
|
|
|
|
static void rxrpc_free_preparse(struct key_preparsed_payload *prep)
|
|
|
|
{
|
2015-10-21 21:04:48 +08:00
|
|
|
rxrpc_free_token_list(prep->payload.data[0]);
|
2014-07-19 01:56:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Preparse a server secret key.
|
|
|
|
*
|
|
|
|
* The data should be the 8-byte secret key.
|
|
|
|
*/
|
|
|
|
static int rxrpc_preparse_s(struct key_preparsed_payload *prep)
|
2007-04-27 06:48:28 +08:00
|
|
|
{
|
2016-01-24 21:19:01 +08:00
|
|
|
struct crypto_skcipher *ci;
|
2007-04-27 06:48:28 +08:00
|
|
|
|
2014-07-19 01:56:36 +08:00
|
|
|
_enter("%zu", prep->datalen);
|
2007-04-27 06:48:28 +08:00
|
|
|
|
2012-09-13 20:06:29 +08:00
|
|
|
if (prep->datalen != 8)
|
2007-04-27 06:48:28 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2015-10-21 21:04:48 +08:00
|
|
|
memcpy(&prep->payload.data[2], prep->data, 8);
|
2007-04-27 06:48:28 +08:00
|
|
|
|
2016-01-24 21:19:01 +08:00
|
|
|
ci = crypto_alloc_skcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC);
|
2007-04-27 06:48:28 +08:00
|
|
|
if (IS_ERR(ci)) {
|
|
|
|
_leave(" = %ld", PTR_ERR(ci));
|
|
|
|
return PTR_ERR(ci);
|
|
|
|
}
|
|
|
|
|
2016-01-24 21:19:01 +08:00
|
|
|
if (crypto_skcipher_setkey(ci, prep->data, 8) < 0)
|
2007-04-27 06:48:28 +08:00
|
|
|
BUG();
|
|
|
|
|
2015-10-21 21:04:48 +08:00
|
|
|
prep->payload.data[0] = ci;
|
2007-04-27 06:48:28 +08:00
|
|
|
_leave(" = 0");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-19 01:56:36 +08:00
|
|
|
/*
|
|
|
|
* Clean up preparse data.
|
|
|
|
*/
|
|
|
|
static void rxrpc_free_preparse_s(struct key_preparsed_payload *prep)
|
|
|
|
{
|
2015-10-21 21:04:48 +08:00
|
|
|
if (prep->payload.data[0])
|
2016-01-24 21:19:01 +08:00
|
|
|
crypto_free_skcipher(prep->payload.data[0]);
|
2014-07-19 01:56:36 +08:00
|
|
|
}
|
|
|
|
|
2007-04-27 06:48:28 +08:00
|
|
|
/*
|
|
|
|
* dispose of the data dangling from the corpse of a rxrpc key
|
|
|
|
*/
|
|
|
|
static void rxrpc_destroy(struct key *key)
|
|
|
|
{
|
2015-10-21 21:04:48 +08:00
|
|
|
rxrpc_free_token_list(key->payload.data[0]);
|
2007-04-27 06:48:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dispose of the data dangling from the corpse of a rxrpc key
|
|
|
|
*/
|
|
|
|
static void rxrpc_destroy_s(struct key *key)
|
|
|
|
{
|
2015-10-21 21:04:48 +08:00
|
|
|
if (key->payload.data[0]) {
|
2016-01-24 21:19:01 +08:00
|
|
|
crypto_free_skcipher(key->payload.data[0]);
|
2015-10-21 21:04:48 +08:00
|
|
|
key->payload.data[0] = NULL;
|
2007-04-27 06:48:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* describe the rxrpc key
|
|
|
|
*/
|
|
|
|
static void rxrpc_describe(const struct key *key, struct seq_file *m)
|
|
|
|
{
|
|
|
|
seq_puts(m, key->description);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* grab the security key for a socket
|
|
|
|
*/
|
2020-07-23 14:09:07 +08:00
|
|
|
int rxrpc_request_key(struct rxrpc_sock *rx, sockptr_t optval, int optlen)
|
2007-04-27 06:48:28 +08:00
|
|
|
{
|
|
|
|
struct key *key;
|
|
|
|
char *description;
|
|
|
|
|
|
|
|
_enter("");
|
|
|
|
|
|
|
|
if (optlen <= 0 || optlen > PAGE_SIZE - 1)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2020-07-23 14:09:07 +08:00
|
|
|
description = memdup_sockptr_nul(optval, optlen);
|
2015-12-24 13:06:05 +08:00
|
|
|
if (IS_ERR(description))
|
|
|
|
return PTR_ERR(description);
|
2007-04-27 06:48:28 +08:00
|
|
|
|
2019-07-11 09:43:43 +08:00
|
|
|
key = request_key_net(&key_type_rxrpc, description, sock_net(&rx->sk), NULL);
|
2007-04-27 06:48:28 +08: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* grab the security keyring for a server socket
|
|
|
|
*/
|
2020-07-23 14:09:07 +08:00
|
|
|
int rxrpc_server_keyring(struct rxrpc_sock *rx, sockptr_t optval, int optlen)
|
2007-04-27 06:48:28 +08:00
|
|
|
{
|
|
|
|
struct key *key;
|
|
|
|
char *description;
|
|
|
|
|
|
|
|
_enter("");
|
|
|
|
|
|
|
|
if (optlen <= 0 || optlen > PAGE_SIZE - 1)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2020-07-23 14:09:07 +08:00
|
|
|
description = memdup_sockptr_nul(optval, optlen);
|
2015-12-24 13:06:05 +08:00
|
|
|
if (IS_ERR(description))
|
|
|
|
return PTR_ERR(description);
|
2007-04-27 06:48:28 +08:00
|
|
|
|
2019-07-11 09:43:43 +08:00
|
|
|
key = request_key_net(&key_type_keyring, description, sock_net(&rx->sk), NULL);
|
2007-04-27 06:48:28 +08:00
|
|
|
if (IS_ERR(key)) {
|
|
|
|
kfree(description);
|
|
|
|
_leave(" = %ld", PTR_ERR(key));
|
|
|
|
return PTR_ERR(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
rx->securities = 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 17:15:40 +08:00
|
|
|
time64_t expiry,
|
2007-04-27 06:48:28 +08: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-14 07:39:23 +08:00
|
|
|
const struct cred *cred = current_cred();
|
2007-04-27 06:48:28 +08:00
|
|
|
struct key *key;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
u32 kver;
|
2009-09-14 09:17:35 +08:00
|
|
|
struct rxrpc_key_data_v1 v1;
|
2007-04-27 06:48:28 +08:00
|
|
|
} data;
|
|
|
|
|
|
|
|
_enter("");
|
|
|
|
|
2012-05-26 06:37:54 +08:00
|
|
|
key = key_alloc(&key_type_rxrpc, "x",
|
2019-07-11 09:43:43 +08: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 23:14:24 +08:00
|
|
|
KEY_ALLOC_NOT_IN_QUOTA, NULL);
|
2007-04-27 06:48:28 +08: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 09:17:35 +08:00
|
|
|
data.v1.security_index = RXRPC_SECURITY_RXKAD;
|
|
|
|
data.v1.ticket_length = 0;
|
2017-08-29 17:15:40 +08:00
|
|
|
data.v1.expiry = rxrpc_time64_to_u32(expiry);
|
2009-09-14 09:17:35 +08:00
|
|
|
data.v1.kvno = 0;
|
2007-04-27 06:48:28 +08:00
|
|
|
|
2009-09-14 09:17:35 +08:00
|
|
|
memcpy(&data.v1.session_key, session_key, sizeof(data.v1.session_key));
|
2007-04-27 06:48:28 +08:00
|
|
|
|
|
|
|
ret = key_instantiate_and_link(key, &data, sizeof(data), NULL, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
2016-04-04 21:00:36 +08:00
|
|
|
conn->params.key = key;
|
2007-04-27 06:48:28 +08: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 14:29:46 +08: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-14 07:39:23 +08:00
|
|
|
const struct cred *cred = current_cred();
|
2007-10-17 14:29:46 +08:00
|
|
|
struct key *key;
|
|
|
|
int ret;
|
|
|
|
|
2012-05-26 06:37:54 +08:00
|
|
|
key = key_alloc(&key_type_rxrpc, keyname,
|
|
|
|
GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, cred,
|
2019-07-11 09:43:43 +08:00
|
|
|
KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA, NULL);
|
2007-10-17 14:29:46 +08: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 09:17:40 +08: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 09:11:24 +08:00
|
|
|
char *buffer, size_t buflen)
|
2009-09-14 09:17:40 +08:00
|
|
|
{
|
2009-09-14 09:17:46 +08:00
|
|
|
const struct rxrpc_key_token *token;
|
|
|
|
const struct krb5_principal *princ;
|
|
|
|
size_t size;
|
2020-03-22 09:11:24 +08:00
|
|
|
__be32 *xdr, *oldxdr;
|
2009-09-14 09:17:46 +08:00
|
|
|
u32 cnlen, toksize, ntoks, tok, zero;
|
|
|
|
u16 toksizes[AFSTOKEN_MAX];
|
|
|
|
int loop;
|
2009-09-14 09:17:40 +08: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 09:17:46 +08:00
|
|
|
#define RND(X) (((X) + 3) & ~3)
|
|
|
|
|
2009-09-14 09:17:40 +08: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 09:17:46 +08:00
|
|
|
size += RND(cnlen); /* cellname */
|
2009-09-14 09:17:40 +08:00
|
|
|
size += 1 * 4; /* token count */
|
|
|
|
|
|
|
|
ntoks = 0;
|
2015-10-21 21:04:48 +08:00
|
|
|
for (token = key->payload.data[0]; token; token = token->next) {
|
2009-09-14 09:17:46 +08:00
|
|
|
toksize = 4; /* sec index */
|
|
|
|
|
2009-09-14 09:17:40 +08:00
|
|
|
switch (token->security_index) {
|
|
|
|
case RXRPC_SECURITY_RXKAD:
|
2020-09-05 01:01:24 +08:00
|
|
|
toksize += 8 * 4; /* viceid, kvno, key*2, begin,
|
2009-09-14 09:17:46 +08:00
|
|
|
* end, primary, tktlen */
|
|
|
|
toksize += RND(token->kad->ticket_len);
|
2009-09-14 09:17:40 +08:00
|
|
|
break;
|
|
|
|
|
2009-09-14 09:17:46 +08:00
|
|
|
case RXRPC_SECURITY_RXK5:
|
|
|
|
princ = &token->k5->client;
|
|
|
|
toksize += 4 + princ->n_name_parts * 4;
|
|
|
|
for (loop = 0; loop < princ->n_name_parts; loop++)
|
|
|
|
toksize += RND(strlen(princ->name_parts[loop]));
|
|
|
|
toksize += 4 + RND(strlen(princ->realm));
|
|
|
|
|
|
|
|
princ = &token->k5->server;
|
|
|
|
toksize += 4 + princ->n_name_parts * 4;
|
|
|
|
for (loop = 0; loop < princ->n_name_parts; loop++)
|
|
|
|
toksize += RND(strlen(princ->name_parts[loop]));
|
|
|
|
toksize += 4 + RND(strlen(princ->realm));
|
|
|
|
|
|
|
|
toksize += 8 + RND(token->k5->session.data_len);
|
|
|
|
|
|
|
|
toksize += 4 * 8 + 2 * 4;
|
|
|
|
|
|
|
|
toksize += 4 + token->k5->n_addresses * 8;
|
|
|
|
for (loop = 0; loop < token->k5->n_addresses; loop++)
|
|
|
|
toksize += RND(token->k5->addresses[loop].data_len);
|
|
|
|
|
|
|
|
toksize += 4 + RND(token->k5->ticket_len);
|
|
|
|
toksize += 4 + RND(token->k5->ticket2_len);
|
|
|
|
|
|
|
|
toksize += 4 + token->k5->n_authdata * 8;
|
|
|
|
for (loop = 0; loop < token->k5->n_authdata; loop++)
|
|
|
|
toksize += RND(token->k5->authdata[loop].data_len);
|
2009-09-14 09:17:40 +08:00
|
|
|
break;
|
2009-09-14 09:17:46 +08:00
|
|
|
|
|
|
|
default: /* we have a ticket we can't encode */
|
2020-09-09 05:09:04 +08:00
|
|
|
pr_err("Unsupported key token type (%u)\n",
|
|
|
|
token->security_index);
|
2009-09-14 09:17:46 +08:00
|
|
|
continue;
|
2009-09-14 09:17:40 +08:00
|
|
|
}
|
2009-09-14 09:17:46 +08:00
|
|
|
|
|
|
|
_debug("token[%u]: toksize=%u", ntoks, toksize);
|
|
|
|
ASSERTCMP(toksize, <=, AFSTOKEN_LENGTH_MAX);
|
|
|
|
|
|
|
|
toksizes[ntoks++] = toksize;
|
|
|
|
size += toksize + 4; /* each token has a length word */
|
2009-09-14 09:17:40 +08:00
|
|
|
}
|
|
|
|
|
2009-09-14 09:17:46 +08:00
|
|
|
#undef RND
|
|
|
|
|
2009-09-14 09:17:40 +08:00
|
|
|
if (!buffer || buflen < size)
|
|
|
|
return size;
|
|
|
|
|
2020-03-22 09:11:24 +08:00
|
|
|
xdr = (__be32 *)buffer;
|
2009-09-14 09:17:40 +08:00
|
|
|
zero = 0;
|
|
|
|
#define ENCODE(x) \
|
|
|
|
do { \
|
2020-03-22 09:11:24 +08:00
|
|
|
*xdr++ = htonl(x); \
|
2009-09-14 09:17:40 +08:00
|
|
|
} while(0)
|
2009-09-14 09:17:46 +08:00
|
|
|
#define ENCODE_DATA(l, s) \
|
|
|
|
do { \
|
|
|
|
u32 _l = (l); \
|
|
|
|
ENCODE(l); \
|
2020-03-22 09:11:24 +08:00
|
|
|
memcpy(xdr, (s), _l); \
|
|
|
|
if (_l & 3) \
|
|
|
|
memcpy((u8 *)xdr + _l, &zero, 4 - (_l & 3)); \
|
2009-09-14 09:17:46 +08:00
|
|
|
xdr += (_l + 3) >> 2; \
|
|
|
|
} while(0)
|
2020-09-05 01:01:24 +08: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 09:17:46 +08:00
|
|
|
#define ENCODE64(x) \
|
|
|
|
do { \
|
|
|
|
__be64 y = cpu_to_be64(x); \
|
2020-03-22 09:11:24 +08:00
|
|
|
memcpy(xdr, &y, 8); \
|
2009-09-14 09:17:46 +08: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 09:17:40 +08:00
|
|
|
|
2009-09-14 09:17:46 +08:00
|
|
|
ENCODE(0); /* flags */
|
|
|
|
ENCODE_DATA(cnlen, key->description + 4); /* cellname */
|
|
|
|
ENCODE(ntoks);
|
2009-09-14 09:17:40 +08:00
|
|
|
|
2009-09-14 09:17:46 +08:00
|
|
|
tok = 0;
|
2015-10-21 21:04:48 +08:00
|
|
|
for (token = key->payload.data[0]; token; token = token->next) {
|
2009-09-14 09:17:46 +08:00
|
|
|
toksize = toksizes[tok++];
|
|
|
|
ENCODE(toksize);
|
|
|
|
oldxdr = xdr;
|
|
|
|
ENCODE(token->security_index);
|
2009-09-14 09:17:40 +08:00
|
|
|
|
|
|
|
switch (token->security_index) {
|
|
|
|
case RXRPC_SECURITY_RXKAD:
|
|
|
|
ENCODE(token->kad->vice_id);
|
|
|
|
ENCODE(token->kad->kvno);
|
2020-09-05 01:01:24 +08:00
|
|
|
ENCODE_BYTES(8, token->kad->session_key);
|
2009-09-14 09:17:40 +08:00
|
|
|
ENCODE(token->kad->start);
|
|
|
|
ENCODE(token->kad->expiry);
|
|
|
|
ENCODE(token->kad->primary_flag);
|
2009-09-14 09:17:46 +08:00
|
|
|
ENCODE_DATA(token->kad->ticket_len, token->kad->ticket);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RXRPC_SECURITY_RXK5:
|
|
|
|
princ = &token->k5->client;
|
|
|
|
ENCODE(princ->n_name_parts);
|
|
|
|
for (loop = 0; loop < princ->n_name_parts; loop++)
|
|
|
|
ENCODE_STR(princ->name_parts[loop]);
|
|
|
|
ENCODE_STR(princ->realm);
|
|
|
|
|
|
|
|
princ = &token->k5->server;
|
|
|
|
ENCODE(princ->n_name_parts);
|
|
|
|
for (loop = 0; loop < princ->n_name_parts; loop++)
|
|
|
|
ENCODE_STR(princ->name_parts[loop]);
|
|
|
|
ENCODE_STR(princ->realm);
|
|
|
|
|
|
|
|
ENCODE(token->k5->session.tag);
|
|
|
|
ENCODE_DATA(token->k5->session.data_len,
|
|
|
|
token->k5->session.data);
|
|
|
|
|
|
|
|
ENCODE64(token->k5->authtime);
|
|
|
|
ENCODE64(token->k5->starttime);
|
|
|
|
ENCODE64(token->k5->endtime);
|
|
|
|
ENCODE64(token->k5->renew_till);
|
|
|
|
ENCODE(token->k5->is_skey);
|
|
|
|
ENCODE(token->k5->flags);
|
|
|
|
|
|
|
|
ENCODE(token->k5->n_addresses);
|
|
|
|
for (loop = 0; loop < token->k5->n_addresses; loop++) {
|
|
|
|
ENCODE(token->k5->addresses[loop].tag);
|
|
|
|
ENCODE_DATA(token->k5->addresses[loop].data_len,
|
|
|
|
token->k5->addresses[loop].data);
|
|
|
|
}
|
|
|
|
|
|
|
|
ENCODE_DATA(token->k5->ticket_len, token->k5->ticket);
|
|
|
|
ENCODE_DATA(token->k5->ticket2_len, token->k5->ticket2);
|
|
|
|
|
|
|
|
ENCODE(token->k5->n_authdata);
|
|
|
|
for (loop = 0; loop < token->k5->n_authdata; loop++) {
|
|
|
|
ENCODE(token->k5->authdata[loop].tag);
|
|
|
|
ENCODE_DATA(token->k5->authdata[loop].data_len,
|
|
|
|
token->k5->authdata[loop].data);
|
|
|
|
}
|
2009-09-14 09:17:40 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-09-14 09:17:46 +08:00
|
|
|
|
|
|
|
ASSERTCMP((unsigned long)xdr - (unsigned long)oldxdr, ==,
|
|
|
|
toksize);
|
2009-09-14 09:17:40 +08:00
|
|
|
}
|
|
|
|
|
2009-09-14 09:17:46 +08:00
|
|
|
#undef ENCODE_STR
|
|
|
|
#undef ENCODE_DATA
|
|
|
|
#undef ENCODE64
|
2009-09-14 09:17:40 +08:00
|
|
|
#undef ENCODE
|
|
|
|
|
2009-09-14 09:17:46 +08:00
|
|
|
ASSERTCMP(tok, ==, ntoks);
|
2009-09-14 09:17:40 +08:00
|
|
|
ASSERTCMP((char __user *) xdr - buffer, ==, size);
|
|
|
|
_leave(" = %zu", size);
|
|
|
|
return size;
|
|
|
|
}
|