···6969 /* Self-signed certificates form roots of their own, and if we7070 * don't know them, then we can't accept them.7171 */7272- if (x509->next == x509) {7272+ if (x509->signer == x509) {7373 kleave(" = -ENOKEY [unknown self-signed]");7474 return -ENOKEY;7575 }
+3-6
crypto/asymmetric_keys/pkcs7_verify.c
···5959 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;60606161 /* Digest the message [RFC2315 9.3] */6262- ret = crypto_shash_init(desc);6363- if (ret < 0)6464- goto error;6565- ret = crypto_shash_finup(desc, pkcs7->data, pkcs7->data_len,6666- sig->digest);6262+ ret = crypto_shash_digest(desc, pkcs7->data, pkcs7->data_len,6363+ sig->digest);6764 if (ret < 0)6865 goto error;6966 pr_devel("MsgDigest = [%*ph]\n", 8, sig->digest);···147150 pr_devel("Sig %u: Found cert serial match X.509[%u]\n",148151 sinfo->index, certix);149152150150- if (x509->pub->pkey_algo != sinfo->sig->pkey_algo) {153153+ if (strcmp(x509->pub->pkey_algo, sinfo->sig->pkey_algo) != 0) {151154 pr_warn("Sig %u: X.509 algo and PKCS#7 sig algo don't match\n",152155 sinfo->index);153156 continue;
+5-2
crypto/asymmetric_keys/public_key.c
···7373 char alg_name_buf[CRYPTO_MAX_ALG_NAME];7474 void *output;7575 unsigned int outlen;7676- int ret = -ENOMEM;7676+ int ret;77777878 pr_devel("==>%s()\n", __func__);7979···9999 if (IS_ERR(tfm))100100 return PTR_ERR(tfm);101101102102+ ret = -ENOMEM;102103 req = akcipher_request_alloc(tfm, GFP_KERNEL);103104 if (!req)104105 goto error_free_tfm;···128127 * signature and returns that to us.129128 */130129 ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);131131- if (ret < 0)130130+ if (ret)132131 goto out_free_output;133132134133 /* Do the actual verification step. */···143142error_free_tfm:144143 crypto_free_akcipher(tfm);145144 pr_devel("<==%s() = %d\n", __func__, ret);145145+ if (WARN_ON_ONCE(ret > 0))146146+ ret = -EINVAL;146147 return ret;147148}148149EXPORT_SYMBOL_GPL(public_key_verify_signature);
+2
crypto/asymmetric_keys/x509_cert_parser.c
···409409 ctx->cert->pub->pkey_algo = "rsa";410410411411 /* Discard the BIT STRING metadata */412412+ if (vlen < 1 || *(const u8 *)value != 0)413413+ return -EBADMSG;412414 ctx->key = value + 1;413415 ctx->key_size = vlen - 1;414416 return 0;
+2-6
crypto/asymmetric_keys/x509_public_key.c
···7979 desc->tfm = tfm;8080 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;81818282- ret = crypto_shash_init(desc);8383- if (ret < 0)8484- goto error_2;8585- might_sleep();8686- ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, sig->digest);8282+ ret = crypto_shash_digest(desc, cert->tbs, cert->tbs_size, sig->digest);8783 if (ret < 0)8884 goto error_2;8985···131135 }132136133137 ret = -EKEYREJECTED;134134- if (cert->pub->pkey_algo != cert->sig->pkey_algo)138138+ if (strcmp(cert->pub->pkey_algo, cert->sig->pkey_algo) != 0)135139 goto out;136140137141 ret = public_key_verify_signature(cert->pub, cert->sig);
+28-21
lib/asn1_decoder.c
···313313314314 /* Decide how to handle the operation */315315 switch (op) {316316- case ASN1_OP_MATCH_ANY_ACT:317317- case ASN1_OP_MATCH_ANY_ACT_OR_SKIP:318318- case ASN1_OP_COND_MATCH_ANY_ACT:319319- case ASN1_OP_COND_MATCH_ANY_ACT_OR_SKIP:320320- ret = actions[machine[pc + 1]](context, hdr, tag, data + dp, len);321321- if (ret < 0)322322- return ret;323323- goto skip_data;324324-325325- case ASN1_OP_MATCH_ACT:326326- case ASN1_OP_MATCH_ACT_OR_SKIP:327327- case ASN1_OP_COND_MATCH_ACT_OR_SKIP:328328- ret = actions[machine[pc + 2]](context, hdr, tag, data + dp, len);329329- if (ret < 0)330330- return ret;331331- goto skip_data;332332-333316 case ASN1_OP_MATCH:334317 case ASN1_OP_MATCH_OR_SKIP:318318+ case ASN1_OP_MATCH_ACT:319319+ case ASN1_OP_MATCH_ACT_OR_SKIP:335320 case ASN1_OP_MATCH_ANY:336321 case ASN1_OP_MATCH_ANY_OR_SKIP:322322+ case ASN1_OP_MATCH_ANY_ACT:323323+ case ASN1_OP_MATCH_ANY_ACT_OR_SKIP:337324 case ASN1_OP_COND_MATCH_OR_SKIP:325325+ case ASN1_OP_COND_MATCH_ACT_OR_SKIP:338326 case ASN1_OP_COND_MATCH_ANY:339327 case ASN1_OP_COND_MATCH_ANY_OR_SKIP:340340- skip_data:328328+ case ASN1_OP_COND_MATCH_ANY_ACT:329329+ case ASN1_OP_COND_MATCH_ANY_ACT_OR_SKIP:330330+341331 if (!(flags & FLAG_CONS)) {342332 if (flags & FLAG_INDEFINITE_LENGTH) {333333+ size_t tmp = dp;334334+343335 ret = asn1_find_indefinite_length(344344- data, datalen, &dp, &len, &errmsg);336336+ data, datalen, &tmp, &len, &errmsg);345337 if (ret < 0)346338 goto error;347347- } else {348348- dp += len;349339 }350340 pr_debug("- LEAF: %zu\n", len);351341 }342342+343343+ if (op & ASN1_OP_MATCH__ACT) {344344+ unsigned char act;345345+346346+ if (op & ASN1_OP_MATCH__ANY)347347+ act = machine[pc + 1];348348+ else349349+ act = machine[pc + 2];350350+ ret = actions[act](context, hdr, tag, data + dp, len);351351+ if (ret < 0)352352+ return ret;353353+ }354354+355355+ if (!(flags & FLAG_CONS))356356+ dp += len;352357 pc += asn1_op_lengths[op];353358 goto next_op;354359···439434 else440435 act = machine[pc + 1];441436 ret = actions[act](context, hdr, 0, data + tdp, len);437437+ if (ret < 0)438438+ return ret;442439 }443440 pc += asn1_op_lengths[op];444441 goto next_op;
+10-6
lib/oid_registry.c
···116116 int count;117117118118 if (v >= end)119119- return -EBADMSG;119119+ goto bad;120120121121 n = *v++;122122 ret = count = snprintf(buffer, bufsize, "%u.%u", n / 40, n % 40);123123+ if (count >= bufsize)124124+ return -ENOBUFS;123125 buffer += count;124126 bufsize -= count;125125- if (bufsize == 0)126126- return -ENOBUFS;127127128128 while (v < end) {129129 num = 0;···134134 num = n & 0x7f;135135 do {136136 if (v >= end)137137- return -EBADMSG;137137+ goto bad;138138 n = *v++;139139 num <<= 7;140140 num |= n & 0x7f;141141 } while (n & 0x80);142142 }143143 ret += count = snprintf(buffer, bufsize, ".%lu", num);144144- buffer += count;145145- if (bufsize <= count)144144+ if (count >= bufsize)146145 return -ENOBUFS;146146+ buffer += count;147147 bufsize -= count;148148 }149149150150 return ret;151151+152152+bad:153153+ snprintf(buffer, bufsize, "(bad)");154154+ return -EBADMSG;151155}152156EXPORT_SYMBOL_GPL(sprint_oid);153157
···15881588 * The caller must have Setattr permission to change keyring restrictions.15891589 *15901590 * The requested type name may be a NULL pointer to reject all attempts15911591- * to link to the keyring. If _type is non-NULL, _restriction can be15921592- * NULL or a pointer to a string describing the restriction. If _type is15931593- * NULL, _restriction must also be NULL.15911591+ * to link to the keyring. In this case, _restriction must also be NULL.15921592+ * Otherwise, both _type and _restriction must be non-NULL.15941593 *15951594 * Returns 0 if successful.15961595 */···15971598 const char __user *_restriction)15981599{15991600 key_ref_t key_ref;16001600- bool link_reject = !_type;16011601 char type[32];16021602 char *restriction = NULL;16031603 long ret;···16051607 if (IS_ERR(key_ref))16061608 return PTR_ERR(key_ref);1607160916101610+ ret = -EINVAL;16081611 if (_type) {16121612+ if (!_restriction)16131613+ goto error;16141614+16091615 ret = key_get_type_from_user(type, _type, sizeof(type));16101616 if (ret < 0)16111617 goto error;16121612- }16131613-16141614- if (_restriction) {16151615- if (!_type) {16161616- ret = -EINVAL;16171617- goto error;16181618- }1619161816201619 restriction = strndup_user(_restriction, PAGE_SIZE);16211620 if (IS_ERR(restriction)) {16221621 ret = PTR_ERR(restriction);16231622 goto error;16241623 }16241624+ } else {16251625+ if (_restriction)16261626+ goto error;16251627 }1626162816271627- ret = keyring_restrict(key_ref, link_reject ? NULL : type, restriction);16291629+ ret = keyring_restrict(key_ref, _type ? type : NULL, restriction);16281630 kfree(restriction);16291629-16301631error:16311632 key_ref_put(key_ref);16321632-16331633 return ret;16341634}16351635
+37-11
security/keys/request_key.c
···251251 * The keyring selected is returned with an extra reference upon it which the252252 * caller must release.253253 */254254-static void construct_get_dest_keyring(struct key **_dest_keyring)254254+static int construct_get_dest_keyring(struct key **_dest_keyring)255255{256256 struct request_key_auth *rka;257257 const struct cred *cred = current_cred();258258 struct key *dest_keyring = *_dest_keyring, *authkey;259259+ int ret;259260260261 kenter("%p", dest_keyring);261262···265264 /* the caller supplied one */266265 key_get(dest_keyring);267266 } else {267267+ bool do_perm_check = true;268268+268269 /* use a default keyring; falling through the cases until we269270 * find one that we actually have */270271 switch (cred->jit_keyring) {···281278 dest_keyring =282279 key_get(rka->dest_keyring);283280 up_read(&authkey->sem);284284- if (dest_keyring)281281+ if (dest_keyring) {282282+ do_perm_check = false;285283 break;284284+ }286285 }287286288287 case KEY_REQKEY_DEFL_THREAD_KEYRING:···319314 default:320315 BUG();321316 }317317+318318+ /*319319+ * Require Write permission on the keyring. This is essential320320+ * because the default keyring may be the session keyring, and321321+ * joining a keyring only requires Search permission.322322+ *323323+ * However, this check is skipped for the "requestor keyring" so324324+ * that /sbin/request-key can itself use request_key() to add325325+ * keys to the original requestor's destination keyring.326326+ */327327+ if (dest_keyring && do_perm_check) {328328+ ret = key_permission(make_key_ref(dest_keyring, 1),329329+ KEY_NEED_WRITE);330330+ if (ret) {331331+ key_put(dest_keyring);332332+ return ret;333333+ }334334+ }322335 }323336324337 *_dest_keyring = dest_keyring;325338 kleave(" [dk %d]", key_serial(dest_keyring));326326- return;339339+ return 0;327340}328341329342/*···467444 if (ctx->index_key.type == &key_type_keyring)468445 return ERR_PTR(-EPERM);469446470470- user = key_user_lookup(current_fsuid());471471- if (!user)472472- return ERR_PTR(-ENOMEM);447447+ ret = construct_get_dest_keyring(&dest_keyring);448448+ if (ret)449449+ goto error;473450474474- construct_get_dest_keyring(&dest_keyring);451451+ user = key_user_lookup(current_fsuid());452452+ if (!user) {453453+ ret = -ENOMEM;454454+ goto error_put_dest_keyring;455455+ }475456476457 ret = construct_alloc_key(ctx, dest_keyring, flags, user, &key);477458 key_user_put(user);···490463 } else if (ret == -EINPROGRESS) {491464 ret = 0;492465 } else {493493- goto couldnt_alloc_key;466466+ goto error_put_dest_keyring;494467 }495468496469 key_put(dest_keyring);···500473construction_failed:501474 key_negate_and_link(key, key_negative_timeout, NULL, NULL);502475 key_put(key);503503-couldnt_alloc_key:476476+error_put_dest_keyring:504477 key_put(dest_keyring);478478+error:505479 kleave(" = %d", ret);506480 return ERR_PTR(ret);507481}···574546 if (!IS_ERR(key_ref)) {575547 key = key_ref_to_ptr(key_ref);576548 if (dest_keyring) {577577- construct_get_dest_keyring(&dest_keyring);578549 ret = key_link(dest_keyring, key);579579- key_put(dest_keyring);580550 if (ret < 0) {581551 key_put(key);582552 key = ERR_PTR(ret);