2019-05-23 03:14:41 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-01-11 07:57:09 -07:00
|
|
|
/* SCTP kernel implementation
|
2007-10-09 02:15:59 -06:00
|
|
|
* (C) Copyright 2007 Hewlett-Packard Development Company, L.P.
|
|
|
|
*
|
2008-01-11 07:57:09 -07:00
|
|
|
* This file is part of the SCTP kernel implementation
|
2007-10-09 02:15:59 -06:00
|
|
|
*
|
|
|
|
* Please send any bug reports or fixes you make to the
|
|
|
|
* email address(es):
|
2013-07-23 06:51:47 -06:00
|
|
|
* lksctp developers <linux-sctp@vger.kernel.org>
|
2007-10-09 02:15:59 -06:00
|
|
|
*
|
|
|
|
* Written or modified by:
|
|
|
|
* Vlad Yasevich <vladislav.yasevich@hp.com>
|
|
|
|
*/
|
|
|
|
|
2016-01-24 06:20:12 -07:00
|
|
|
#include <crypto/hash.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 02:04:11 -06:00
|
|
|
#include <linux/slab.h>
|
2007-10-09 02:15:59 -06:00
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/scatterlist.h>
|
|
|
|
#include <net/sctp/sctp.h>
|
|
|
|
#include <net/sctp/auth.h>
|
|
|
|
|
|
|
|
static struct sctp_hmac sctp_hmac_list[SCTP_AUTH_NUM_HMACS] = {
|
|
|
|
{
|
|
|
|
/* id 0 is reserved. as all 0 */
|
|
|
|
.hmac_id = SCTP_AUTH_HMAC_ID_RESERVED_0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.hmac_id = SCTP_AUTH_HMAC_ID_SHA1,
|
2013-12-22 21:16:50 -07:00
|
|
|
.hmac_name = "hmac(sha1)",
|
2007-10-09 02:15:59 -06:00
|
|
|
.hmac_len = SCTP_SHA1_SIG_SIZE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
/* id 2 is reserved as well */
|
|
|
|
.hmac_id = SCTP_AUTH_HMAC_ID_RESERVED_2,
|
|
|
|
},
|
2016-09-09 06:43:19 -06:00
|
|
|
#if IS_ENABLED(CONFIG_CRYPTO_SHA256)
|
2007-10-09 02:15:59 -06:00
|
|
|
{
|
|
|
|
.hmac_id = SCTP_AUTH_HMAC_ID_SHA256,
|
2013-12-22 21:16:50 -07:00
|
|
|
.hmac_name = "hmac(sha256)",
|
2007-10-09 02:15:59 -06:00
|
|
|
.hmac_len = SCTP_SHA256_SIG_SIZE,
|
|
|
|
}
|
2007-11-29 07:53:52 -07:00
|
|
|
#endif
|
2007-10-09 02:15:59 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void sctp_auth_key_put(struct sctp_auth_bytes *key)
|
|
|
|
{
|
|
|
|
if (!key)
|
|
|
|
return;
|
|
|
|
|
2017-07-04 06:53:24 -06:00
|
|
|
if (refcount_dec_and_test(&key->refcnt)) {
|
2013-02-06 17:55:37 -07:00
|
|
|
kzfree(key);
|
2007-10-09 02:15:59 -06:00
|
|
|
SCTP_DBG_OBJCNT_DEC(keys);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new key structure of a given length */
|
|
|
|
static struct sctp_auth_bytes *sctp_auth_create_key(__u32 key_len, gfp_t gfp)
|
|
|
|
{
|
|
|
|
struct sctp_auth_bytes *key;
|
|
|
|
|
2008-08-25 16:16:19 -06:00
|
|
|
/* Verify that we are not going to overflow INT_MAX */
|
2011-11-29 02:26:30 -07:00
|
|
|
if (key_len > (INT_MAX - sizeof(struct sctp_auth_bytes)))
|
2008-08-25 16:16:19 -06:00
|
|
|
return NULL;
|
|
|
|
|
2007-10-09 02:15:59 -06:00
|
|
|
/* Allocate the shared key */
|
|
|
|
key = kmalloc(sizeof(struct sctp_auth_bytes) + key_len, gfp);
|
|
|
|
if (!key)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
key->len = key_len;
|
2017-07-04 06:53:24 -06:00
|
|
|
refcount_set(&key->refcnt, 1);
|
2007-10-09 02:15:59 -06:00
|
|
|
SCTP_DBG_OBJCNT_INC(keys);
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new shared key container with a give key id */
|
|
|
|
struct sctp_shared_key *sctp_auth_shkey_create(__u16 key_id, gfp_t gfp)
|
|
|
|
{
|
|
|
|
struct sctp_shared_key *new;
|
|
|
|
|
|
|
|
/* Allocate the shared key container */
|
|
|
|
new = kzalloc(sizeof(struct sctp_shared_key), gfp);
|
|
|
|
if (!new)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&new->key_list);
|
2018-03-14 05:05:30 -06:00
|
|
|
refcount_set(&new->refcnt, 1);
|
2007-10-09 02:15:59 -06:00
|
|
|
new->key_id = key_id;
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2011-03-30 19:57:33 -06:00
|
|
|
/* Free the shared key structure */
|
2018-03-14 05:05:30 -06:00
|
|
|
static void sctp_auth_shkey_destroy(struct sctp_shared_key *sh_key)
|
2007-10-09 02:15:59 -06:00
|
|
|
{
|
|
|
|
BUG_ON(!list_empty(&sh_key->key_list));
|
|
|
|
sctp_auth_key_put(sh_key->key);
|
|
|
|
sh_key->key = NULL;
|
|
|
|
kfree(sh_key);
|
|
|
|
}
|
|
|
|
|
2018-03-14 05:05:30 -06:00
|
|
|
void sctp_auth_shkey_release(struct sctp_shared_key *sh_key)
|
|
|
|
{
|
|
|
|
if (refcount_dec_and_test(&sh_key->refcnt))
|
|
|
|
sctp_auth_shkey_destroy(sh_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sctp_auth_shkey_hold(struct sctp_shared_key *sh_key)
|
|
|
|
{
|
|
|
|
refcount_inc(&sh_key->refcnt);
|
|
|
|
}
|
|
|
|
|
2011-03-30 19:57:33 -06:00
|
|
|
/* Destroy the entire key list. This is done during the
|
2007-10-09 02:15:59 -06:00
|
|
|
* associon and endpoint free process.
|
|
|
|
*/
|
|
|
|
void sctp_auth_destroy_keys(struct list_head *keys)
|
|
|
|
{
|
|
|
|
struct sctp_shared_key *ep_key;
|
|
|
|
struct sctp_shared_key *tmp;
|
|
|
|
|
|
|
|
if (list_empty(keys))
|
|
|
|
return;
|
|
|
|
|
|
|
|
key_for_each_safe(ep_key, tmp, keys) {
|
|
|
|
list_del_init(&ep_key->key_list);
|
2018-03-14 05:05:30 -06:00
|
|
|
sctp_auth_shkey_release(ep_key);
|
2007-10-09 02:15:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compare two byte vectors as numbers. Return values
|
|
|
|
* are:
|
|
|
|
* 0 - vectors are equal
|
2008-10-16 11:02:37 -06:00
|
|
|
* < 0 - vector 1 is smaller than vector2
|
|
|
|
* > 0 - vector 1 is greater than vector2
|
2007-10-09 02:15:59 -06:00
|
|
|
*
|
|
|
|
* Algorithm is:
|
|
|
|
* This is performed by selecting the numerically smaller key vector...
|
|
|
|
* If the key vectors are equal as numbers but differ in length ...
|
|
|
|
* the shorter vector is considered smaller
|
|
|
|
*
|
|
|
|
* Examples (with small values):
|
|
|
|
* 000123456789 > 123456789 (first number is longer)
|
|
|
|
* 000123456789 < 234567891 (second number is larger numerically)
|
|
|
|
* 123456789 > 2345678 (first number is both larger & longer)
|
|
|
|
*/
|
|
|
|
static int sctp_auth_compare_vectors(struct sctp_auth_bytes *vector1,
|
|
|
|
struct sctp_auth_bytes *vector2)
|
|
|
|
{
|
|
|
|
int diff;
|
|
|
|
int i;
|
|
|
|
const __u8 *longer;
|
|
|
|
|
|
|
|
diff = vector1->len - vector2->len;
|
|
|
|
if (diff) {
|
|
|
|
longer = (diff > 0) ? vector1->data : vector2->data;
|
|
|
|
|
|
|
|
/* Check to see if the longer number is
|
|
|
|
* lead-zero padded. If it is not, it
|
|
|
|
* is automatically larger numerically.
|
|
|
|
*/
|
2013-12-22 21:16:50 -07:00
|
|
|
for (i = 0; i < abs(diff); i++) {
|
2007-10-09 02:15:59 -06:00
|
|
|
if (longer[i] != 0)
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lengths are the same, compare numbers */
|
|
|
|
return memcmp(vector1->data, vector2->data, vector1->len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a key vector as described in SCTP-AUTH, Section 6.1
|
|
|
|
* The RANDOM parameter, the CHUNKS parameter and the HMAC-ALGO
|
|
|
|
* parameter sent by each endpoint are concatenated as byte vectors.
|
|
|
|
* These parameters include the parameter type, parameter length, and
|
|
|
|
* the parameter value, but padding is omitted; all padding MUST be
|
|
|
|
* removed from this concatenation before proceeding with further
|
|
|
|
* computation of keys. Parameters which were not sent are simply
|
|
|
|
* omitted from the concatenation process. The resulting two vectors
|
|
|
|
* are called the two key vectors.
|
|
|
|
*/
|
|
|
|
static struct sctp_auth_bytes *sctp_auth_make_key_vector(
|
2017-07-16 21:29:57 -06:00
|
|
|
struct sctp_random_param *random,
|
2017-07-16 21:29:58 -06:00
|
|
|
struct sctp_chunks_param *chunks,
|
2017-07-16 21:29:59 -06:00
|
|
|
struct sctp_hmac_algo_param *hmacs,
|
2007-10-09 02:15:59 -06:00
|
|
|
gfp_t gfp)
|
|
|
|
{
|
|
|
|
struct sctp_auth_bytes *new;
|
|
|
|
__u32 len;
|
|
|
|
__u32 offset = 0;
|
2013-02-06 18:41:39 -07:00
|
|
|
__u16 random_len, hmacs_len, chunks_len = 0;
|
2007-10-09 02:15:59 -06:00
|
|
|
|
2013-02-06 18:41:39 -07:00
|
|
|
random_len = ntohs(random->param_hdr.length);
|
|
|
|
hmacs_len = ntohs(hmacs->param_hdr.length);
|
|
|
|
if (chunks)
|
|
|
|
chunks_len = ntohs(chunks->param_hdr.length);
|
|
|
|
|
|
|
|
len = random_len + hmacs_len + chunks_len;
|
2007-10-09 02:15:59 -06:00
|
|
|
|
2013-02-07 16:22:58 -07:00
|
|
|
new = sctp_auth_create_key(len, gfp);
|
2007-10-09 02:15:59 -06:00
|
|
|
if (!new)
|
|
|
|
return NULL;
|
|
|
|
|
2013-02-06 18:41:39 -07:00
|
|
|
memcpy(new->data, random, random_len);
|
|
|
|
offset += random_len;
|
2007-10-09 02:15:59 -06:00
|
|
|
|
|
|
|
if (chunks) {
|
2013-02-06 18:41:39 -07:00
|
|
|
memcpy(new->data + offset, chunks, chunks_len);
|
|
|
|
offset += chunks_len;
|
2007-10-09 02:15:59 -06:00
|
|
|
}
|
|
|
|
|
2013-02-06 18:41:39 -07:00
|
|
|
memcpy(new->data + offset, hmacs, hmacs_len);
|
2007-10-09 02:15:59 -06:00
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Make a key vector based on our local parameters */
|
2007-10-26 05:21:23 -06:00
|
|
|
static struct sctp_auth_bytes *sctp_auth_make_local_vector(
|
2007-10-09 02:15:59 -06:00
|
|
|
const struct sctp_association *asoc,
|
|
|
|
gfp_t gfp)
|
|
|
|
{
|
|
|
|
return sctp_auth_make_key_vector(
|
2017-07-16 21:29:57 -06:00
|
|
|
(struct sctp_random_param *)asoc->c.auth_random,
|
2017-07-16 21:29:58 -06:00
|
|
|
(struct sctp_chunks_param *)asoc->c.auth_chunks,
|
2017-07-16 21:29:59 -06:00
|
|
|
(struct sctp_hmac_algo_param *)asoc->c.auth_hmacs, gfp);
|
2007-10-09 02:15:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make a key vector based on peer's parameters */
|
2007-10-26 05:21:23 -06:00
|
|
|
static struct sctp_auth_bytes *sctp_auth_make_peer_vector(
|
2007-10-09 02:15:59 -06:00
|
|
|
const struct sctp_association *asoc,
|
|
|
|
gfp_t gfp)
|
|
|
|
{
|
|
|
|
return sctp_auth_make_key_vector(asoc->peer.peer_random,
|
|
|
|
asoc->peer.peer_chunks,
|
|
|
|
asoc->peer.peer_hmacs,
|
|
|
|
gfp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Set the value of the association shared key base on the parameters
|
|
|
|
* given. The algorithm is:
|
|
|
|
* From the endpoint pair shared keys and the key vectors the
|
|
|
|
* association shared keys are computed. This is performed by selecting
|
|
|
|
* the numerically smaller key vector and concatenating it to the
|
|
|
|
* endpoint pair shared key, and then concatenating the numerically
|
|
|
|
* larger key vector to that. The result of the concatenation is the
|
|
|
|
* association shared key.
|
|
|
|
*/
|
|
|
|
static struct sctp_auth_bytes *sctp_auth_asoc_set_secret(
|
|
|
|
struct sctp_shared_key *ep_key,
|
|
|
|
struct sctp_auth_bytes *first_vector,
|
|
|
|
struct sctp_auth_bytes *last_vector,
|
|
|
|
gfp_t gfp)
|
|
|
|
{
|
|
|
|
struct sctp_auth_bytes *secret;
|
|
|
|
__u32 offset = 0;
|
|
|
|
__u32 auth_len;
|
|
|
|
|
|
|
|
auth_len = first_vector->len + last_vector->len;
|
|
|
|
if (ep_key->key)
|
|
|
|
auth_len += ep_key->key->len;
|
|
|
|
|
|
|
|
secret = sctp_auth_create_key(auth_len, gfp);
|
|
|
|
if (!secret)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (ep_key->key) {
|
|
|
|
memcpy(secret->data, ep_key->key->data, ep_key->key->len);
|
|
|
|
offset += ep_key->key->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(secret->data + offset, first_vector->data, first_vector->len);
|
|
|
|
offset += first_vector->len;
|
|
|
|
|
|
|
|
memcpy(secret->data + offset, last_vector->data, last_vector->len);
|
|
|
|
|
|
|
|
return secret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create an association shared key. Follow the algorithm
|
|
|
|
* described in SCTP-AUTH, Section 6.1
|
|
|
|
*/
|
|
|
|
static struct sctp_auth_bytes *sctp_auth_asoc_create_secret(
|
|
|
|
const struct sctp_association *asoc,
|
|
|
|
struct sctp_shared_key *ep_key,
|
|
|
|
gfp_t gfp)
|
|
|
|
{
|
|
|
|
struct sctp_auth_bytes *local_key_vector;
|
|
|
|
struct sctp_auth_bytes *peer_key_vector;
|
|
|
|
struct sctp_auth_bytes *first_vector,
|
|
|
|
*last_vector;
|
|
|
|
struct sctp_auth_bytes *secret = NULL;
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
|
|
|
|
/* Now we need to build the key vectors
|
|
|
|
* SCTP-AUTH , Section 6.1
|
|
|
|
* The RANDOM parameter, the CHUNKS parameter and the HMAC-ALGO
|
|
|
|
* parameter sent by each endpoint are concatenated as byte vectors.
|
|
|
|
* These parameters include the parameter type, parameter length, and
|
|
|
|
* the parameter value, but padding is omitted; all padding MUST be
|
|
|
|
* removed from this concatenation before proceeding with further
|
|
|
|
* computation of keys. Parameters which were not sent are simply
|
|
|
|
* omitted from the concatenation process. The resulting two vectors
|
|
|
|
* are called the two key vectors.
|
|
|
|
*/
|
|
|
|
|
|
|
|
local_key_vector = sctp_auth_make_local_vector(asoc, gfp);
|
|
|
|
peer_key_vector = sctp_auth_make_peer_vector(asoc, gfp);
|
|
|
|
|
|
|
|
if (!peer_key_vector || !local_key_vector)
|
|
|
|
goto out;
|
|
|
|
|
2011-03-30 19:57:33 -06:00
|
|
|
/* Figure out the order in which the key_vectors will be
|
2007-10-09 02:15:59 -06:00
|
|
|
* added to the endpoint shared key.
|
|
|
|
* SCTP-AUTH, Section 6.1:
|
|
|
|
* This is performed by selecting the numerically smaller key
|
|
|
|
* vector and concatenating it to the endpoint pair shared
|
|
|
|
* key, and then concatenating the numerically larger key
|
|
|
|
* vector to that. If the key vectors are equal as numbers
|
|
|
|
* but differ in length, then the concatenation order is the
|
|
|
|
* endpoint shared key, followed by the shorter key vector,
|
|
|
|
* followed by the longer key vector. Otherwise, the key
|
|
|
|
* vectors are identical, and may be concatenated to the
|
|
|
|
* endpoint pair key in any order.
|
|
|
|
*/
|
|
|
|
cmp = sctp_auth_compare_vectors(local_key_vector,
|
|
|
|
peer_key_vector);
|
|
|
|
if (cmp < 0) {
|
|
|
|
first_vector = local_key_vector;
|
|
|
|
last_vector = peer_key_vector;
|
|
|
|
} else {
|
|
|
|
first_vector = peer_key_vector;
|
|
|
|
last_vector = local_key_vector;
|
|
|
|
}
|
|
|
|
|
|
|
|
secret = sctp_auth_asoc_set_secret(ep_key, first_vector, last_vector,
|
|
|
|
gfp);
|
|
|
|
out:
|
2013-02-07 16:22:58 -07:00
|
|
|
sctp_auth_key_put(local_key_vector);
|
|
|
|
sctp_auth_key_put(peer_key_vector);
|
2007-10-09 02:15:59 -06:00
|
|
|
|
|
|
|
return secret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Populate the association overlay list with the list
|
|
|
|
* from the endpoint.
|
|
|
|
*/
|
|
|
|
int sctp_auth_asoc_copy_shkeys(const struct sctp_endpoint *ep,
|
|
|
|
struct sctp_association *asoc,
|
|
|
|
gfp_t gfp)
|
|
|
|
{
|
|
|
|
struct sctp_shared_key *sh_key;
|
|
|
|
struct sctp_shared_key *new;
|
|
|
|
|
|
|
|
BUG_ON(!list_empty(&asoc->endpoint_shared_keys));
|
|
|
|
|
|
|
|
key_for_each(sh_key, &ep->endpoint_shared_keys) {
|
|
|
|
new = sctp_auth_shkey_create(sh_key->key_id, gfp);
|
|
|
|
if (!new)
|
|
|
|
goto nomem;
|
|
|
|
|
|
|
|
new->key = sh_key->key;
|
|
|
|
sctp_auth_key_hold(new->key);
|
|
|
|
list_add(&new->key_list, &asoc->endpoint_shared_keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
nomem:
|
|
|
|
sctp_auth_destroy_keys(&asoc->endpoint_shared_keys);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-11 11:49:46 -06:00
|
|
|
/* Public interface to create the association shared key.
|
2007-10-09 02:15:59 -06:00
|
|
|
* See code above for the algorithm.
|
|
|
|
*/
|
|
|
|
int sctp_auth_asoc_init_active_key(struct sctp_association *asoc, gfp_t gfp)
|
|
|
|
{
|
|
|
|
struct sctp_auth_bytes *secret;
|
|
|
|
struct sctp_shared_key *ep_key;
|
2015-06-11 11:49:46 -06:00
|
|
|
struct sctp_chunk *chunk;
|
2007-10-09 02:15:59 -06:00
|
|
|
|
|
|
|
/* If we don't support AUTH, or peer is not capable
|
|
|
|
* we don't need to do anything.
|
|
|
|
*/
|
2019-08-19 08:02:47 -06:00
|
|
|
if (!asoc->peer.auth_capable)
|
2007-10-09 02:15:59 -06:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* If the key_id is non-zero and we couldn't find an
|
|
|
|
* endpoint pair shared key, we can't compute the
|
|
|
|
* secret.
|
|
|
|
* For key_id 0, endpoint pair shared key is a NULL key.
|
|
|
|
*/
|
|
|
|
ep_key = sctp_auth_get_shkey(asoc, asoc->active_key_id);
|
|
|
|
BUG_ON(!ep_key);
|
|
|
|
|
|
|
|
secret = sctp_auth_asoc_create_secret(asoc, ep_key, gfp);
|
|
|
|
if (!secret)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
sctp_auth_key_put(asoc->asoc_shared_key);
|
|
|
|
asoc->asoc_shared_key = secret;
|
2018-03-14 05:05:30 -06:00
|
|
|
asoc->shkey = ep_key;
|
2007-10-09 02:15:59 -06:00
|
|
|
|
2015-06-11 11:49:46 -06:00
|
|
|
/* Update send queue in case any chunk already in there now
|
|
|
|
* needs authenticating
|
|
|
|
*/
|
|
|
|
list_for_each_entry(chunk, &asoc->outqueue.out_chunk_list, list) {
|
2018-03-14 05:05:30 -06:00
|
|
|
if (sctp_auth_send_cid(chunk->chunk_hdr->type, asoc)) {
|
2015-06-11 11:49:46 -06:00
|
|
|
chunk->auth = 1;
|
2018-03-14 05:05:30 -06:00
|
|
|
if (!chunk->shkey) {
|
|
|
|
chunk->shkey = asoc->shkey;
|
|
|
|
sctp_auth_shkey_hold(chunk->shkey);
|
|
|
|
}
|
|
|
|
}
|
2015-06-11 11:49:46 -06:00
|
|
|
}
|
|
|
|
|
2007-10-09 02:15:59 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Find the endpoint pair shared key based on the key_id */
|
|
|
|
struct sctp_shared_key *sctp_auth_get_shkey(
|
|
|
|
const struct sctp_association *asoc,
|
|
|
|
__u16 key_id)
|
|
|
|
{
|
[SCTP]: Fix kernel panic while received AUTH chunk with BAD shared key identifier
If SCTP-AUTH is enabled, received AUTH chunk with BAD shared key
identifier will cause kernel panic.
Test as following:
step1: enabled /proc/sys/net/sctp/auth_enable
step 2: connect to SCTP server with auth capable. Association is
established between endpoints. Then send a AUTH chunk with a bad
shareid, SCTP server will kernel panic after received that AUTH chunk.
SCTP client SCTP server
INIT ---------->
(with auth capable)
<---------- INIT-ACK
(with auth capable)
COOKIE-ECHO ---------->
<---------- COOKIE-ACK
AUTH ---------->
AUTH chunk is like this:
AUTH chunk
Chunk type: AUTH (15)
Chunk flags: 0x00
Chunk length: 28
Shared key identifier: 10
HMAC identifier: SHA-1 (1)
HMAC: 0000000000000000000000000000000000000000
The assignment of NULL to key can safely be removed, since key_for_each
(which is just list_for_each_entry under the covers does an initial
assignment to key anyway).
If the endpoint_shared_keys list is empty, or if the key_id being
requested does not exist, the function as it currently stands returns
the actuall list_head (in this case endpoint_shared_keys. Since that
list_head isn't surrounded by an actuall data structure, the last
iteration through list_for_each_entry will do a container_of on key, and
we wind up returning a bogus pointer, instead of NULL, as we should.
> Neil Horman wrote:
>> On Tue, Jan 22, 2008 at 05:29:20PM +0900, Wei Yongjun wrote:
>>
>> FWIW, Ack from me. The assignment of NULL to key can safely be
>> removed, since
>> key_for_each (which is just list_for_each_entry under the covers does
>> an initial
>> assignment to key anyway).
>> If the endpoint_shared_keys list is empty, or if the key_id being
>> requested does
>> not exist, the function as it currently stands returns the actuall
>> list_head (in
>> this case endpoint_shared_keys. Since that list_head isn't
>> surrounded by an
>> actuall data structure, the last iteration through
>> list_for_each_entry will do a
>> container_of on key, and we wind up returning a bogus pointer,
>> instead of NULL,
>> as we should. Wei's patch corrects that.
>>
>> Regards
>> Neil
>>
>> Acked-by: Neil Horman <nhorman@tuxdriver.com>
>>
>
> Yep, the patch is correct.
>
> Acked-by: Vlad Yasevich <vladislav.yasevich@hp.com>
>
> -vlad
>
Signed-off-by: Wei Yongjun <yjwei@cn.fujitsu.com>
Acked-by: Neil Horman <nhorman@tuxdriver.com>
Acked-by: Vlad Yasevich <vladislav.yasevich@hp.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2008-02-05 04:03:06 -07:00
|
|
|
struct sctp_shared_key *key;
|
2007-10-09 02:15:59 -06:00
|
|
|
|
|
|
|
/* First search associations set of endpoint pair shared keys */
|
|
|
|
key_for_each(key, &asoc->endpoint_shared_keys) {
|
2018-03-14 05:05:32 -06:00
|
|
|
if (key->key_id == key_id) {
|
|
|
|
if (!key->deactivated)
|
|
|
|
return key;
|
|
|
|
break;
|
|
|
|
}
|
2007-10-09 02:15:59 -06:00
|
|
|
}
|
|
|
|
|
[SCTP]: Fix kernel panic while received AUTH chunk with BAD shared key identifier
If SCTP-AUTH is enabled, received AUTH chunk with BAD shared key
identifier will cause kernel panic.
Test as following:
step1: enabled /proc/sys/net/sctp/auth_enable
step 2: connect to SCTP server with auth capable. Association is
established between endpoints. Then send a AUTH chunk with a bad
shareid, SCTP server will kernel panic after received that AUTH chunk.
SCTP client SCTP server
INIT ---------->
(with auth capable)
<---------- INIT-ACK
(with auth capable)
COOKIE-ECHO ---------->
<---------- COOKIE-ACK
AUTH ---------->
AUTH chunk is like this:
AUTH chunk
Chunk type: AUTH (15)
Chunk flags: 0x00
Chunk length: 28
Shared key identifier: 10
HMAC identifier: SHA-1 (1)
HMAC: 0000000000000000000000000000000000000000
The assignment of NULL to key can safely be removed, since key_for_each
(which is just list_for_each_entry under the covers does an initial
assignment to key anyway).
If the endpoint_shared_keys list is empty, or if the key_id being
requested does not exist, the function as it currently stands returns
the actuall list_head (in this case endpoint_shared_keys. Since that
list_head isn't surrounded by an actuall data structure, the last
iteration through list_for_each_entry will do a container_of on key, and
we wind up returning a bogus pointer, instead of NULL, as we should.
> Neil Horman wrote:
>> On Tue, Jan 22, 2008 at 05:29:20PM +0900, Wei Yongjun wrote:
>>
>> FWIW, Ack from me. The assignment of NULL to key can safely be
>> removed, since
>> key_for_each (which is just list_for_each_entry under the covers does
>> an initial
>> assignment to key anyway).
>> If the endpoint_shared_keys list is empty, or if the key_id being
>> requested does
>> not exist, the function as it currently stands returns the actuall
>> list_head (in
>> this case endpoint_shared_keys. Since that list_head isn't
>> surrounded by an
>> actuall data structure, the last iteration through
>> list_for_each_entry will do a
>> container_of on key, and we wind up returning a bogus pointer,
>> instead of NULL,
>> as we should. Wei's patch corrects that.
>>
>> Regards
>> Neil
>>
>> Acked-by: Neil Horman <nhorman@tuxdriver.com>
>>
>
> Yep, the patch is correct.
>
> Acked-by: Vlad Yasevich <vladislav.yasevich@hp.com>
>
> -vlad
>
Signed-off-by: Wei Yongjun <yjwei@cn.fujitsu.com>
Acked-by: Neil Horman <nhorman@tuxdriver.com>
Acked-by: Vlad Yasevich <vladislav.yasevich@hp.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2008-02-05 04:03:06 -07:00
|
|
|
return NULL;
|
2007-10-09 02:15:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize all the possible digest transforms that we can use. Right now
|
|
|
|
* now, the supported digests are SHA1 and SHA256. We do this here once
|
|
|
|
* because of the restrictiong that transforms may only be allocated in
|
|
|
|
* user context. This forces us to pre-allocated all possible transforms
|
|
|
|
* at the endpoint init time.
|
|
|
|
*/
|
|
|
|
int sctp_auth_init_hmacs(struct sctp_endpoint *ep, gfp_t gfp)
|
|
|
|
{
|
2016-01-24 06:20:12 -07:00
|
|
|
struct crypto_shash *tfm = NULL;
|
2007-10-09 02:15:59 -06:00
|
|
|
__u16 id;
|
|
|
|
|
net: sctp: cache auth_enable per endpoint
Currently, it is possible to create an SCTP socket, then switch
auth_enable via sysctl setting to 1 and crash the system on connect:
Oops[#1]:
CPU: 0 PID: 0 Comm: swapper Not tainted 3.14.1-mipsgit-20140415 #1
task: ffffffff8056ce80 ti: ffffffff8055c000 task.ti: ffffffff8055c000
[...]
Call Trace:
[<ffffffff8043c4e8>] sctp_auth_asoc_set_default_hmac+0x68/0x80
[<ffffffff8042b300>] sctp_process_init+0x5e0/0x8a4
[<ffffffff8042188c>] sctp_sf_do_5_1B_init+0x234/0x34c
[<ffffffff804228c8>] sctp_do_sm+0xb4/0x1e8
[<ffffffff80425a08>] sctp_endpoint_bh_rcv+0x1c4/0x214
[<ffffffff8043af68>] sctp_rcv+0x588/0x630
[<ffffffff8043e8e8>] sctp6_rcv+0x10/0x24
[<ffffffff803acb50>] ip6_input+0x2c0/0x440
[<ffffffff8030fc00>] __netif_receive_skb_core+0x4a8/0x564
[<ffffffff80310650>] process_backlog+0xb4/0x18c
[<ffffffff80313cbc>] net_rx_action+0x12c/0x210
[<ffffffff80034254>] __do_softirq+0x17c/0x2ac
[<ffffffff800345e0>] irq_exit+0x54/0xb0
[<ffffffff800075a4>] ret_from_irq+0x0/0x4
[<ffffffff800090ec>] rm7k_wait_irqoff+0x24/0x48
[<ffffffff8005e388>] cpu_startup_entry+0xc0/0x148
[<ffffffff805a88b0>] start_kernel+0x37c/0x398
Code: dd0900b8 000330f8 0126302d <dcc60000> 50c0fff1 0047182a a48306a0
03e00008 00000000
---[ end trace b530b0551467f2fd ]---
Kernel panic - not syncing: Fatal exception in interrupt
What happens while auth_enable=0 in that case is, that
ep->auth_hmacs is initialized to NULL in sctp_auth_init_hmacs()
when endpoint is being created.
After that point, if an admin switches over to auth_enable=1,
the machine can crash due to NULL pointer dereference during
reception of an INIT chunk. When we enter sctp_process_init()
via sctp_sf_do_5_1B_init() in order to respond to an INIT chunk,
the INIT verification succeeds and while we walk and process
all INIT params via sctp_process_param() we find that
net->sctp.auth_enable is set, therefore do not fall through,
but invoke sctp_auth_asoc_set_default_hmac() instead, and thus,
dereference what we have set to NULL during endpoint
initialization phase.
The fix is to make auth_enable immutable by caching its value
during endpoint initialization, so that its original value is
being carried along until destruction. The bug seems to originate
from the very first days.
Fix in joint work with Daniel Borkmann.
Reported-by: Joshua Kinard <kumba@gentoo.org>
Signed-off-by: Vlad Yasevich <vyasevic@redhat.com>
Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
Acked-by: Neil Horman <nhorman@tuxdriver.com>
Tested-by: Joshua Kinard <kumba@gentoo.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2014-04-17 09:26:50 -06:00
|
|
|
/* If the transforms are already allocated, we are done */
|
2007-10-09 02:15:59 -06:00
|
|
|
if (ep->auth_hmacs)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Allocated the array of pointers to transorms */
|
treewide: kzalloc() -> kcalloc()
The kzalloc() function has a 2-factor argument form, kcalloc(). This
patch replaces cases of:
kzalloc(a * b, gfp)
with:
kcalloc(a * b, gfp)
as well as handling cases of:
kzalloc(a * b * c, gfp)
with:
kzalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kzalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kzalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kzalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kzalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kzalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kzalloc
+ kcalloc
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kzalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kzalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kzalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kzalloc(sizeof(THING) * C2, ...)
|
kzalloc(sizeof(TYPE) * C2, ...)
|
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(C1 * C2, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * E2
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 15:03:40 -06:00
|
|
|
ep->auth_hmacs = kcalloc(SCTP_AUTH_NUM_HMACS,
|
|
|
|
sizeof(struct crypto_shash *),
|
|
|
|
gfp);
|
2007-10-09 02:15:59 -06:00
|
|
|
if (!ep->auth_hmacs)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (id = 0; id < SCTP_AUTH_NUM_HMACS; id++) {
|
|
|
|
|
|
|
|
/* See is we support the id. Supported IDs have name and
|
|
|
|
* length fields set, so that we can allocated and use
|
|
|
|
* them. We can safely just check for name, for without the
|
|
|
|
* name, we can't allocate the TFM.
|
|
|
|
*/
|
|
|
|
if (!sctp_hmac_list[id].hmac_name)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* If this TFM has been allocated, we are all set */
|
|
|
|
if (ep->auth_hmacs[id])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Allocate the ID */
|
2016-01-24 06:20:12 -07:00
|
|
|
tfm = crypto_alloc_shash(sctp_hmac_list[id].hmac_name, 0, 0);
|
2007-10-09 02:15:59 -06:00
|
|
|
if (IS_ERR(tfm))
|
|
|
|
goto out_err;
|
|
|
|
|
|
|
|
ep->auth_hmacs[id] = tfm;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_err:
|
2009-01-07 19:09:16 -07:00
|
|
|
/* Clean up any successful allocations */
|
2007-10-09 02:15:59 -06:00
|
|
|
sctp_auth_destroy_hmacs(ep->auth_hmacs);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Destroy the hmac tfm array */
|
2016-01-24 06:20:12 -07:00
|
|
|
void sctp_auth_destroy_hmacs(struct crypto_shash *auth_hmacs[])
|
2007-10-09 02:15:59 -06:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!auth_hmacs)
|
|
|
|
return;
|
|
|
|
|
2013-12-22 21:16:53 -07:00
|
|
|
for (i = 0; i < SCTP_AUTH_NUM_HMACS; i++) {
|
2016-01-24 06:20:12 -07:00
|
|
|
crypto_free_shash(auth_hmacs[i]);
|
2007-10-09 02:15:59 -06:00
|
|
|
}
|
|
|
|
kfree(auth_hmacs);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct sctp_hmac *sctp_auth_get_hmac(__u16 hmac_id)
|
|
|
|
{
|
|
|
|
return &sctp_hmac_list[hmac_id];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get an hmac description information that we can use to build
|
|
|
|
* the AUTH chunk
|
|
|
|
*/
|
|
|
|
struct sctp_hmac *sctp_auth_asoc_get_hmac(const struct sctp_association *asoc)
|
|
|
|
{
|
|
|
|
struct sctp_hmac_algo_param *hmacs;
|
|
|
|
__u16 n_elt;
|
|
|
|
__u16 id = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* If we have a default entry, use it */
|
|
|
|
if (asoc->default_hmac_id)
|
|
|
|
return &sctp_hmac_list[asoc->default_hmac_id];
|
|
|
|
|
|
|
|
/* Since we do not have a default entry, find the first entry
|
|
|
|
* we support and return that. Do not cache that id.
|
|
|
|
*/
|
|
|
|
hmacs = asoc->peer.peer_hmacs;
|
|
|
|
if (!hmacs)
|
|
|
|
return NULL;
|
|
|
|
|
2017-06-29 21:52:16 -06:00
|
|
|
n_elt = (ntohs(hmacs->param_hdr.length) -
|
|
|
|
sizeof(struct sctp_paramhdr)) >> 1;
|
2007-10-09 02:15:59 -06:00
|
|
|
for (i = 0; i < n_elt; i++) {
|
|
|
|
id = ntohs(hmacs->hmac_ids[i]);
|
|
|
|
|
2013-10-26 02:06:32 -06:00
|
|
|
/* Check the id is in the supported range. And
|
|
|
|
* see if we support the id. Supported IDs have name and
|
|
|
|
* length fields set, so that we can allocate and use
|
2007-10-09 02:15:59 -06:00
|
|
|
* them. We can safely just check for name, for without the
|
|
|
|
* name, we can't allocate the TFM.
|
|
|
|
*/
|
2013-10-26 02:06:32 -06:00
|
|
|
if (id > SCTP_AUTH_HMAC_ID_MAX ||
|
|
|
|
!sctp_hmac_list[id].hmac_name) {
|
2010-10-01 05:51:47 -06:00
|
|
|
id = 0;
|
2007-10-09 02:15:59 -06:00
|
|
|
continue;
|
2010-10-01 05:51:47 -06:00
|
|
|
}
|
2007-10-09 02:15:59 -06:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return &sctp_hmac_list[id];
|
|
|
|
}
|
|
|
|
|
2007-10-28 23:03:23 -06:00
|
|
|
static int __sctp_auth_find_hmacid(__be16 *hmacs, int n_elts, __be16 hmac_id)
|
2007-10-09 02:15:59 -06:00
|
|
|
{
|
|
|
|
int found = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < n_elts; i++) {
|
|
|
|
if (hmac_id == hmacs[i]) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See if the HMAC_ID is one that we claim as supported */
|
|
|
|
int sctp_auth_asoc_verify_hmac_id(const struct sctp_association *asoc,
|
2007-10-28 23:03:23 -06:00
|
|
|
__be16 hmac_id)
|
2007-10-09 02:15:59 -06:00
|
|
|
{
|
|
|
|
struct sctp_hmac_algo_param *hmacs;
|
|
|
|
__u16 n_elt;
|
|
|
|
|
|
|
|
if (!asoc)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
hmacs = (struct sctp_hmac_algo_param *)asoc->c.auth_hmacs;
|
2017-06-29 21:52:16 -06:00
|
|
|
n_elt = (ntohs(hmacs->param_hdr.length) -
|
|
|
|
sizeof(struct sctp_paramhdr)) >> 1;
|
2007-10-09 02:15:59 -06:00
|
|
|
|
|
|
|
return __sctp_auth_find_hmacid(hmacs->hmac_ids, n_elt, hmac_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Cache the default HMAC id. This to follow this text from SCTP-AUTH:
|
|
|
|
* Section 6.1:
|
|
|
|
* The receiver of a HMAC-ALGO parameter SHOULD use the first listed
|
|
|
|
* algorithm it supports.
|
|
|
|
*/
|
|
|
|
void sctp_auth_asoc_set_default_hmac(struct sctp_association *asoc,
|
|
|
|
struct sctp_hmac_algo_param *hmacs)
|
|
|
|
{
|
|
|
|
struct sctp_endpoint *ep;
|
|
|
|
__u16 id;
|
|
|
|
int i;
|
|
|
|
int n_params;
|
|
|
|
|
|
|
|
/* if the default id is already set, use it */
|
|
|
|
if (asoc->default_hmac_id)
|
|
|
|
return;
|
|
|
|
|
2017-06-29 21:52:16 -06:00
|
|
|
n_params = (ntohs(hmacs->param_hdr.length) -
|
|
|
|
sizeof(struct sctp_paramhdr)) >> 1;
|
2007-10-09 02:15:59 -06:00
|
|
|
ep = asoc->ep;
|
|
|
|
for (i = 0; i < n_params; i++) {
|
|
|
|
id = ntohs(hmacs->hmac_ids[i]);
|
|
|
|
|
|
|
|
/* Check the id is in the supported range */
|
|
|
|
if (id > SCTP_AUTH_HMAC_ID_MAX)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* If this TFM has been allocated, use this id */
|
|
|
|
if (ep->auth_hmacs[id]) {
|
|
|
|
asoc->default_hmac_id = id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Check to see if the given chunk is supposed to be authenticated */
|
2017-06-29 21:52:14 -06:00
|
|
|
static int __sctp_auth_cid(enum sctp_cid chunk, struct sctp_chunks_param *param)
|
2007-10-09 02:15:59 -06:00
|
|
|
{
|
|
|
|
unsigned short len;
|
|
|
|
int found = 0;
|
|
|
|
int i;
|
|
|
|
|
2007-11-29 06:56:16 -07:00
|
|
|
if (!param || param->param_hdr.length == 0)
|
2007-10-09 02:15:59 -06:00
|
|
|
return 0;
|
|
|
|
|
2017-06-29 21:52:16 -06:00
|
|
|
len = ntohs(param->param_hdr.length) - sizeof(struct sctp_paramhdr);
|
2007-10-09 02:15:59 -06:00
|
|
|
|
|
|
|
/* SCTP-AUTH, Section 3.2
|
|
|
|
* The chunk types for INIT, INIT-ACK, SHUTDOWN-COMPLETE and AUTH
|
|
|
|
* chunks MUST NOT be listed in the CHUNKS parameter. However, if
|
|
|
|
* a CHUNKS parameter is received then the types for INIT, INIT-ACK,
|
|
|
|
* SHUTDOWN-COMPLETE and AUTH chunks MUST be ignored.
|
|
|
|
*/
|
|
|
|
for (i = 0; !found && i < len; i++) {
|
|
|
|
switch (param->chunks[i]) {
|
2013-12-22 21:16:52 -07:00
|
|
|
case SCTP_CID_INIT:
|
|
|
|
case SCTP_CID_INIT_ACK:
|
|
|
|
case SCTP_CID_SHUTDOWN_COMPLETE:
|
|
|
|
case SCTP_CID_AUTH:
|
2007-10-09 02:15:59 -06:00
|
|
|
break;
|
|
|
|
|
2013-12-22 21:16:52 -07:00
|
|
|
default:
|
2007-10-09 02:15:59 -06:00
|
|
|
if (param->chunks[i] == chunk)
|
2013-12-22 21:16:52 -07:00
|
|
|
found = 1;
|
2007-10-09 02:15:59 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if peer requested that this chunk is authenticated */
|
2017-06-29 21:52:14 -06:00
|
|
|
int sctp_auth_send_cid(enum sctp_cid chunk, const struct sctp_association *asoc)
|
2007-10-09 02:15:59 -06:00
|
|
|
{
|
2012-08-07 01:29:57 -06:00
|
|
|
if (!asoc)
|
|
|
|
return 0;
|
|
|
|
|
2019-08-19 08:02:47 -06:00
|
|
|
if (!asoc->peer.auth_capable)
|
2007-10-09 02:15:59 -06:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return __sctp_auth_cid(chunk, asoc->peer.peer_chunks);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we requested that peer authenticate this chunk. */
|
2017-06-29 21:52:14 -06:00
|
|
|
int sctp_auth_recv_cid(enum sctp_cid chunk, const struct sctp_association *asoc)
|
2007-10-09 02:15:59 -06:00
|
|
|
{
|
2012-08-07 01:29:57 -06:00
|
|
|
if (!asoc)
|
|
|
|
return 0;
|
|
|
|
|
2019-08-19 08:02:47 -06:00
|
|
|
if (!asoc->peer.auth_capable)
|
2007-10-09 02:15:59 -06:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return __sctp_auth_cid(chunk,
|
|
|
|
(struct sctp_chunks_param *)asoc->c.auth_chunks);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SCTP-AUTH: Section 6.2:
|
|
|
|
* The sender MUST calculate the MAC as described in RFC2104 [2] using
|
|
|
|
* the hash function H as described by the MAC Identifier and the shared
|
|
|
|
* association key K based on the endpoint pair shared key described by
|
|
|
|
* the shared key identifier. The 'data' used for the computation of
|
|
|
|
* the AUTH-chunk is given by the AUTH chunk with its HMAC field set to
|
|
|
|
* zero (as shown in Figure 6) followed by all chunks that are placed
|
|
|
|
* after the AUTH chunk in the SCTP packet.
|
|
|
|
*/
|
|
|
|
void sctp_auth_calculate_hmac(const struct sctp_association *asoc,
|
2018-03-14 05:05:30 -06:00
|
|
|
struct sk_buff *skb, struct sctp_auth_chunk *auth,
|
|
|
|
struct sctp_shared_key *ep_key, gfp_t gfp)
|
2007-10-09 02:15:59 -06:00
|
|
|
{
|
|
|
|
struct sctp_auth_bytes *asoc_key;
|
2018-03-14 05:05:30 -06:00
|
|
|
struct crypto_shash *tfm;
|
2007-10-09 02:15:59 -06:00
|
|
|
__u16 key_id, hmac_id;
|
|
|
|
unsigned char *end;
|
|
|
|
int free_key = 0;
|
2018-03-14 05:05:30 -06:00
|
|
|
__u8 *digest;
|
2007-10-09 02:15:59 -06:00
|
|
|
|
|
|
|
/* Extract the info we need:
|
|
|
|
* - hmac id
|
|
|
|
* - key id
|
|
|
|
*/
|
|
|
|
key_id = ntohs(auth->auth_hdr.shkey_id);
|
|
|
|
hmac_id = ntohs(auth->auth_hdr.hmac_id);
|
|
|
|
|
|
|
|
if (key_id == asoc->active_key_id)
|
|
|
|
asoc_key = asoc->asoc_shared_key;
|
|
|
|
else {
|
2018-03-14 05:05:30 -06:00
|
|
|
/* ep_key can't be NULL here */
|
2007-10-09 02:15:59 -06:00
|
|
|
asoc_key = sctp_auth_asoc_create_secret(asoc, ep_key, gfp);
|
|
|
|
if (!asoc_key)
|
|
|
|
return;
|
|
|
|
|
|
|
|
free_key = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set up scatter list */
|
|
|
|
end = skb_tail_pointer(skb);
|
|
|
|
|
2016-01-24 06:20:12 -07:00
|
|
|
tfm = asoc->ep->auth_hmacs[hmac_id];
|
2007-10-09 02:15:59 -06:00
|
|
|
|
|
|
|
digest = auth->auth_hdr.hmac;
|
2016-01-24 06:20:12 -07:00
|
|
|
if (crypto_shash_setkey(tfm, &asoc_key->data[0], asoc_key->len))
|
2007-10-09 02:15:59 -06:00
|
|
|
goto free;
|
|
|
|
|
2020-05-01 23:31:20 -06:00
|
|
|
crypto_shash_tfm_digest(tfm, (u8 *)auth, end - (unsigned char *)auth,
|
|
|
|
digest);
|
2007-10-09 02:15:59 -06:00
|
|
|
|
|
|
|
free:
|
|
|
|
if (free_key)
|
|
|
|
sctp_auth_key_put(asoc_key);
|
|
|
|
}
|
2007-09-16 20:34:00 -06:00
|
|
|
|
|
|
|
/* API Helpers */
|
|
|
|
|
|
|
|
/* Add a chunk to the endpoint authenticated chunk list */
|
|
|
|
int sctp_auth_ep_add_chunkid(struct sctp_endpoint *ep, __u8 chunk_id)
|
|
|
|
{
|
|
|
|
struct sctp_chunks_param *p = ep->auth_chunk_list;
|
|
|
|
__u16 nchunks;
|
|
|
|
__u16 param_len;
|
|
|
|
|
|
|
|
/* If this chunk is already specified, we are done */
|
|
|
|
if (__sctp_auth_cid(chunk_id, p))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Check if we can add this chunk to the array */
|
|
|
|
param_len = ntohs(p->param_hdr.length);
|
2017-06-29 21:52:16 -06:00
|
|
|
nchunks = param_len - sizeof(struct sctp_paramhdr);
|
2007-09-16 20:34:00 -06:00
|
|
|
if (nchunks == SCTP_NUM_CHUNK_TYPES)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
p->chunks[nchunks] = chunk_id;
|
|
|
|
p->param_hdr.length = htons(param_len + 1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add hmac identifires to the endpoint list of supported hmac ids */
|
|
|
|
int sctp_auth_ep_set_hmacs(struct sctp_endpoint *ep,
|
|
|
|
struct sctp_hmacalgo *hmacs)
|
|
|
|
{
|
|
|
|
int has_sha1 = 0;
|
|
|
|
__u16 id;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Scan the list looking for unsupported id. Also make sure that
|
|
|
|
* SHA1 is specified.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < hmacs->shmac_num_idents; i++) {
|
|
|
|
id = hmacs->shmac_idents[i];
|
|
|
|
|
2008-08-27 17:09:49 -06:00
|
|
|
if (id > SCTP_AUTH_HMAC_ID_MAX)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2007-09-16 20:34:00 -06:00
|
|
|
if (SCTP_AUTH_HMAC_ID_SHA1 == id)
|
|
|
|
has_sha1 = 1;
|
|
|
|
|
|
|
|
if (!sctp_hmac_list[id].hmac_name)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!has_sha1)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2015-11-11 22:07:07 -07:00
|
|
|
for (i = 0; i < hmacs->shmac_num_idents; i++)
|
2017-06-29 21:52:16 -06:00
|
|
|
ep->auth_hmacs_list->hmac_ids[i] =
|
|
|
|
htons(hmacs->shmac_idents[i]);
|
|
|
|
ep->auth_hmacs_list->param_hdr.length =
|
|
|
|
htons(sizeof(struct sctp_paramhdr) +
|
|
|
|
hmacs->shmac_num_idents * sizeof(__u16));
|
2007-09-16 20:34:00 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set a new shared key on either endpoint or association. If the
|
|
|
|
* the key with a same ID already exists, replace the key (remove the
|
|
|
|
* old key and add a new one).
|
|
|
|
*/
|
|
|
|
int sctp_auth_set_key(struct sctp_endpoint *ep,
|
|
|
|
struct sctp_association *asoc,
|
|
|
|
struct sctp_authkey *auth_key)
|
|
|
|
{
|
2018-03-14 05:05:30 -06:00
|
|
|
struct sctp_shared_key *cur_key, *shkey;
|
2007-09-16 20:34:00 -06:00
|
|
|
struct sctp_auth_bytes *key;
|
|
|
|
struct list_head *sh_keys;
|
|
|
|
int replace = 0;
|
|
|
|
|
|
|
|
/* Try to find the given key id to see if
|
|
|
|
* we are doing a replace, or adding a new key
|
|
|
|
*/
|
2019-08-19 08:02:47 -06:00
|
|
|
if (asoc) {
|
|
|
|
if (!asoc->peer.auth_capable)
|
|
|
|
return -EACCES;
|
2007-09-16 20:34:00 -06:00
|
|
|
sh_keys = &asoc->endpoint_shared_keys;
|
2019-08-19 08:02:47 -06:00
|
|
|
} else {
|
|
|
|
if (!ep->auth_enable)
|
|
|
|
return -EACCES;
|
2007-09-16 20:34:00 -06:00
|
|
|
sh_keys = &ep->endpoint_shared_keys;
|
2019-08-19 08:02:47 -06:00
|
|
|
}
|
2007-09-16 20:34:00 -06:00
|
|
|
|
2018-03-14 05:05:30 -06:00
|
|
|
key_for_each(shkey, sh_keys) {
|
|
|
|
if (shkey->key_id == auth_key->sca_keynumber) {
|
2007-09-16 20:34:00 -06:00
|
|
|
replace = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 05:05:30 -06:00
|
|
|
cur_key = sctp_auth_shkey_create(auth_key->sca_keynumber, GFP_KERNEL);
|
|
|
|
if (!cur_key)
|
|
|
|
return -ENOMEM;
|
2007-09-16 20:34:00 -06:00
|
|
|
|
|
|
|
/* Create a new key data based on the info passed in */
|
2008-02-27 14:04:52 -07:00
|
|
|
key = sctp_auth_create_key(auth_key->sca_keylength, GFP_KERNEL);
|
2018-03-14 05:05:30 -06:00
|
|
|
if (!key) {
|
|
|
|
kfree(cur_key);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2007-09-16 20:34:00 -06:00
|
|
|
|
2008-02-27 14:04:52 -07:00
|
|
|
memcpy(key->data, &auth_key->sca_key[0], auth_key->sca_keylength);
|
2018-03-14 05:05:30 -06:00
|
|
|
cur_key->key = key;
|
2007-09-16 20:34:00 -06:00
|
|
|
|
2018-03-14 05:05:30 -06:00
|
|
|
if (replace) {
|
|
|
|
list_del_init(&shkey->key_list);
|
|
|
|
sctp_auth_shkey_release(shkey);
|
|
|
|
}
|
|
|
|
list_add(&cur_key->key_list, sh_keys);
|
2007-09-16 20:34:00 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sctp_auth_set_active_key(struct sctp_endpoint *ep,
|
|
|
|
struct sctp_association *asoc,
|
|
|
|
__u16 key_id)
|
|
|
|
{
|
|
|
|
struct sctp_shared_key *key;
|
|
|
|
struct list_head *sh_keys;
|
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
/* The key identifier MUST correst to an existing key */
|
2019-08-19 08:02:47 -06:00
|
|
|
if (asoc) {
|
|
|
|
if (!asoc->peer.auth_capable)
|
|
|
|
return -EACCES;
|
2007-09-16 20:34:00 -06:00
|
|
|
sh_keys = &asoc->endpoint_shared_keys;
|
2019-08-19 08:02:47 -06:00
|
|
|
} else {
|
|
|
|
if (!ep->auth_enable)
|
|
|
|
return -EACCES;
|
2007-09-16 20:34:00 -06:00
|
|
|
sh_keys = &ep->endpoint_shared_keys;
|
2019-08-19 08:02:47 -06:00
|
|
|
}
|
2007-09-16 20:34:00 -06:00
|
|
|
|
|
|
|
key_for_each(key, sh_keys) {
|
|
|
|
if (key->key_id == key_id) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 05:05:32 -06:00
|
|
|
if (!found || key->deactivated)
|
2007-09-16 20:34:00 -06:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (asoc) {
|
|
|
|
asoc->active_key_id = key_id;
|
|
|
|
sctp_auth_asoc_init_active_key(asoc, GFP_KERNEL);
|
|
|
|
} else
|
|
|
|
ep->active_key_id = key_id;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int sctp_auth_del_key_id(struct sctp_endpoint *ep,
|
|
|
|
struct sctp_association *asoc,
|
|
|
|
__u16 key_id)
|
|
|
|
{
|
|
|
|
struct sctp_shared_key *key;
|
|
|
|
struct list_head *sh_keys;
|
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
/* The key identifier MUST NOT be the current active key
|
|
|
|
* The key identifier MUST correst to an existing key
|
|
|
|
*/
|
|
|
|
if (asoc) {
|
2019-08-19 08:02:47 -06:00
|
|
|
if (!asoc->peer.auth_capable)
|
|
|
|
return -EACCES;
|
2007-09-16 20:34:00 -06:00
|
|
|
if (asoc->active_key_id == key_id)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
sh_keys = &asoc->endpoint_shared_keys;
|
|
|
|
} else {
|
2019-08-19 08:02:47 -06:00
|
|
|
if (!ep->auth_enable)
|
|
|
|
return -EACCES;
|
2007-09-16 20:34:00 -06:00
|
|
|
if (ep->active_key_id == key_id)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
sh_keys = &ep->endpoint_shared_keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
key_for_each(key, sh_keys) {
|
|
|
|
if (key->key_id == key_id) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Delete the shared key */
|
|
|
|
list_del_init(&key->key_list);
|
2018-03-14 05:05:30 -06:00
|
|
|
sctp_auth_shkey_release(key);
|
2007-09-16 20:34:00 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-03-14 05:05:32 -06:00
|
|
|
|
|
|
|
int sctp_auth_deact_key_id(struct sctp_endpoint *ep,
|
|
|
|
struct sctp_association *asoc, __u16 key_id)
|
|
|
|
{
|
|
|
|
struct sctp_shared_key *key;
|
|
|
|
struct list_head *sh_keys;
|
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
/* The key identifier MUST NOT be the current active key
|
|
|
|
* The key identifier MUST correst to an existing key
|
|
|
|
*/
|
|
|
|
if (asoc) {
|
2019-08-19 08:02:47 -06:00
|
|
|
if (!asoc->peer.auth_capable)
|
|
|
|
return -EACCES;
|
2018-03-14 05:05:32 -06:00
|
|
|
if (asoc->active_key_id == key_id)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
sh_keys = &asoc->endpoint_shared_keys;
|
|
|
|
} else {
|
2019-08-19 08:02:47 -06:00
|
|
|
if (!ep->auth_enable)
|
|
|
|
return -EACCES;
|
2018-03-14 05:05:32 -06:00
|
|
|
if (ep->active_key_id == key_id)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
sh_keys = &ep->endpoint_shared_keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
key_for_each(key, sh_keys) {
|
|
|
|
if (key->key_id == key_id) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2018-03-14 05:05:33 -06:00
|
|
|
/* refcnt == 1 and !list_empty mean it's not being used anywhere
|
|
|
|
* and deactivated will be set, so it's time to notify userland
|
|
|
|
* that this shkey can be freed.
|
|
|
|
*/
|
|
|
|
if (asoc && !list_empty(&key->key_list) &&
|
|
|
|
refcount_read(&key->refcnt) == 1) {
|
|
|
|
struct sctp_ulpevent *ev;
|
|
|
|
|
|
|
|
ev = sctp_ulpevent_make_authkey(asoc, key->key_id,
|
|
|
|
SCTP_AUTH_FREE_KEY, GFP_KERNEL);
|
|
|
|
if (ev)
|
|
|
|
asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
|
|
|
|
}
|
|
|
|
|
2018-03-14 05:05:32 -06:00
|
|
|
key->deactivated = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-08-19 08:02:48 -06:00
|
|
|
|
|
|
|
int sctp_auth_init(struct sctp_endpoint *ep, gfp_t gfp)
|
|
|
|
{
|
|
|
|
int err = -ENOMEM;
|
|
|
|
|
|
|
|
/* Allocate space for HMACS and CHUNKS authentication
|
|
|
|
* variables. There are arrays that we encode directly
|
|
|
|
* into parameters to make the rest of the operations easier.
|
|
|
|
*/
|
|
|
|
if (!ep->auth_hmacs_list) {
|
|
|
|
struct sctp_hmac_algo_param *auth_hmacs;
|
|
|
|
|
|
|
|
auth_hmacs = kzalloc(struct_size(auth_hmacs, hmac_ids,
|
|
|
|
SCTP_AUTH_NUM_HMACS), gfp);
|
|
|
|
if (!auth_hmacs)
|
|
|
|
goto nomem;
|
|
|
|
/* Initialize the HMACS parameter.
|
|
|
|
* SCTP-AUTH: Section 3.3
|
|
|
|
* Every endpoint supporting SCTP chunk authentication MUST
|
|
|
|
* support the HMAC based on the SHA-1 algorithm.
|
|
|
|
*/
|
|
|
|
auth_hmacs->param_hdr.type = SCTP_PARAM_HMAC_ALGO;
|
|
|
|
auth_hmacs->param_hdr.length =
|
|
|
|
htons(sizeof(struct sctp_paramhdr) + 2);
|
|
|
|
auth_hmacs->hmac_ids[0] = htons(SCTP_AUTH_HMAC_ID_SHA1);
|
|
|
|
ep->auth_hmacs_list = auth_hmacs;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ep->auth_chunk_list) {
|
|
|
|
struct sctp_chunks_param *auth_chunks;
|
|
|
|
|
|
|
|
auth_chunks = kzalloc(sizeof(*auth_chunks) +
|
|
|
|
SCTP_NUM_CHUNK_TYPES, gfp);
|
|
|
|
if (!auth_chunks)
|
|
|
|
goto nomem;
|
|
|
|
/* Initialize the CHUNKS parameter */
|
|
|
|
auth_chunks->param_hdr.type = SCTP_PARAM_CHUNKS;
|
|
|
|
auth_chunks->param_hdr.length =
|
|
|
|
htons(sizeof(struct sctp_paramhdr));
|
|
|
|
ep->auth_chunk_list = auth_chunks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate and initialize transorms arrays for supported
|
|
|
|
* HMACs.
|
|
|
|
*/
|
|
|
|
err = sctp_auth_init_hmacs(ep, gfp);
|
|
|
|
if (err)
|
|
|
|
goto nomem;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
nomem:
|
|
|
|
/* Free all allocations */
|
|
|
|
kfree(ep->auth_hmacs_list);
|
|
|
|
kfree(ep->auth_chunk_list);
|
|
|
|
ep->auth_hmacs_list = NULL;
|
|
|
|
ep->auth_chunk_list = NULL;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sctp_auth_free(struct sctp_endpoint *ep)
|
|
|
|
{
|
|
|
|
kfree(ep->auth_hmacs_list);
|
|
|
|
kfree(ep->auth_chunk_list);
|
|
|
|
ep->auth_hmacs_list = NULL;
|
|
|
|
ep->auth_chunk_list = NULL;
|
|
|
|
sctp_auth_destroy_hmacs(ep->auth_hmacs);
|
|
|
|
ep->auth_hmacs = NULL;
|
|
|
|
}
|