Browse Source

[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 years ago
parent
commit
0a4805bf94
1 changed files with 10 additions and 7 deletions
  1. 10
    7
      src/net/peerblk.c

+ 10
- 7
src/net/peerblk.c View File

700
 		return -EPROTO;
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
 	peerblk->cipher = cipher;
706
 	peerblk->cipher = cipher;
707
 	assert ( peerblk->cipherctx == NULL );
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
 				    keylen ) ) != 0 ) {
717
 				    keylen ) ) != 0 ) {
715
 		DBGC ( peerblk, "PEERBLK %p %d.%d could not set key: %s\n",
718
 		DBGC ( peerblk, "PEERBLK %p %d.%d could not set key: %s\n",
716
 		       peerblk, peerblk->segment, peerblk->block,
719
 		       peerblk, peerblk->segment, peerblk->block,

Loading…
Cancel
Save