浏览代码

[peerdist] Avoid NULL pointer dereference for plaintext blocks

Avoid accidentally dereferencing a NULL cipher context pointer for
plaintext blocks (which are usually messages with a block length of
zero, indicating a missing block).

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 9 年前
父节点
当前提交
0a4805bf94
共有 1 个文件被更改,包括 10 次插入7 次删除
  1. 10
    7
      src/net/peerblk.c

+ 10
- 7
src/net/peerblk.c 查看文件

@@ -700,17 +700,20 @@ static int peerblk_parse_header ( struct peerdist_block *peerblk ) {
700 700
 		return -EPROTO;
701 701
 	}
702 702
 
703
-	/* Allocate cipher context.  Freeing the cipher context (on
704
-	 * error or otherwise) is handled by peerblk_reset().
703
+	/* Allocate cipher context, if applicable.  Freeing the cipher
704
+	 * context (on error or otherwise) is handled by peerblk_reset().
705 705
 	 */
706 706
 	peerblk->cipher = cipher;
707 707
 	assert ( peerblk->cipherctx == NULL );
708
-	peerblk->cipherctx = malloc ( cipher->ctxsize );
709
-	if ( ! peerblk->cipherctx )
710
-		return -ENOMEM;
708
+	if ( cipher ) {
709
+		peerblk->cipherctx = malloc ( cipher->ctxsize );
710
+		if ( ! peerblk->cipherctx )
711
+			return -ENOMEM;
712
+	}
711 713
 
712
-	/* Initialise cipher */
713
-	if ( ( rc = cipher_setkey ( cipher, peerblk->cipherctx, peerblk->secret,
714
+	/* Initialise cipher, if applicable */
715
+	if ( cipher &&
716
+	     ( rc = cipher_setkey ( cipher, peerblk->cipherctx, peerblk->secret,
714 717
 				    keylen ) ) != 0 ) {
715 718
 		DBGC ( peerblk, "PEERBLK %p %d.%d could not set key: %s\n",
716 719
 		       peerblk, peerblk->segment, peerblk->block,

正在加载...
取消
保存