nfc: st21nfca: fix incorrect validating logic in EVT_TRANSACTION
Subsystems (2)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH v3 1/3 nfc: st21nfca: fix incorrect validating logic in EVT_TRANSACTION
The first validation check for EVT_TRANSACTION has two different checks tied together with logical AND. One is a check for minimum packet length, and the other is for a valid aid_tag. If either condition is true (fails), then an error should be triggered. The fix is to change && to ||.
--- drivers/nfc/st21nfca/se.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nfc/st21nfca/se.c +1 -1
@@ -315,7 +315,7 @@ int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host,* AID 81 5 to 16 * PARAMETERS 82 0 to 255 */ -if (skb->len <NFC_MIN_AID_LENGTH +2 && +if (skb->len <NFC_MIN_AID_LENGTH +2 || skb->data[0 ] !=NFC_EVT_TRANSACTION_AID_TAG )return -EPROTO ;
Signature · 1 line
2.36.1.255.ge46751e96f-goog
PATCH v3 2/3 nfc: st21nfca: fix memory leaks in EVT_TRANSACTION handling
In reply to a message not in the archiveError paths do not free previously allocated memory. Add devm_kfree() to those failure paths.
--- drivers/nfc/st21nfca/se.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/nfc/st21nfca/se.c +10 -3
@@ -326,22 +326,29 @@ int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host, transaction->aid_len = skb->data[1 ];/* Checking if the length of the AID is valid */ -if (transaction->aid_len >sizeof (transaction->aid)) +if (transaction->aid_len >sizeof (transaction->aid)) { +devm_kfree (dev, transaction);return -EINVAL ; + }memcpy (transaction->aid, &skb->data[2 ], transaction->aid_len);/* Check next byte is PARAMETERS tag (82) */ if (skb->data[transaction->aid_len +2 ] != -NFC_EVT_TRANSACTION_PARAMS_TAG ) +NFC_EVT_TRANSACTION_PARAMS_TAG ) { +devm_kfree (dev, transaction);return -EPROTO ; + } transaction->params_len = skb->data[transaction->aid_len +3 ];/* Total size is allocated (skb->len - 2) minus fixed array members */ -if (transaction->params_len > ((skb->len -2 ) -sizeof (struct nfc_evt_transaction ))) +if (transaction->params_len > ((skb->len -2 ) - +sizeof (struct nfc_evt_transaction ))) { +devm_kfree (dev, transaction);return -EINVAL ; + }memcpy (transaction->params, skb->data + transaction->aid_len +4 , transaction->params_len);
Signature · 1 line
2.36.1.255.ge46751e96f-goog
PATCH v3 3/3 nfc: st21nfca: fix incorrect sizing calculations in EVT_TRANSACTION
In reply to a message not in the archiveThe transaction buffer is allocated by using the size of the packet buf, and subtracting two which seem intended to remove the two tags which are not present in the target structure. This calculation leads to under counting memory because of differences between the packet contents and the target structure. The aid_len field is a u8 in the packet, but a u32 in the structure, resulting in at least 3 bytes always being under counted. Further, the aid data is a variable length field in the packet, but fixed in the structure, so if this field is less than the max, the difference is added to the under counting.
The last validation check for transaction->params_len is also incorrect since it employs the same accounting error.
To fix, perform validation checks progressively to safely reach the next field, to determine the size of both buffers and verify both tags. Once all validation checks pass, allocate the buffer and copy the data. This eliminates freeing memory on the error path, as those checks are moved ahead of memory allocation.
--- drivers/nfc/st21nfca/se.c | 60 +++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 30 deletions(-)
drivers/nfc/st21nfca/se.c +30 -30
@@ -300,6 +300,8 @@ int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host,int r =0 ;struct device *dev = &hdev->ndev->dev;struct nfc_evt_transaction *transaction; +u32 aid_len; +u8 params_len;pr_debug ("connectivity gate event: %x\n" , event);
@@ -308,50 +310,48 @@ int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host, r =nfc_se_connectivity (hdev->ndev, host);break ;case ST21NFCA_EVT_TRANSACTION : -/* -* According to specification etsi 102 622 +/* According to specification etsi 102 622 * 11.2.2.4 EVT_TRANSACTION Table 52 * Description Tag Length * AID 81 5 to 16 * PARAMETERS 82 0 to 255 +* +* The key differences are aid storage length is variably sized +* in the packet, but fixed in nfc_evt_transaction, and that the aid_len +* is u8 in the packet, but u32 in the structure, and the tags in +* the packet are not included in nfc_evt_transaction. +* +* size in bytes: 1 1 5-16 1 1 0-255 +* offset: 0 1 2 aid_len + 2 aid_len + 3 aid_len + 4 +* member name: aid_tag(M) aid_len aid params_tag(M) params_len params +* example: 0x81 5-16 X 0x82 0-255 X */ -if (skb->len <NFC_MIN_AID_LENGTH +2 || - skb->data[0 ] !=NFC_EVT_TRANSACTION_AID_TAG ) +if (skb->len <2 || skb->data[0 ] !=NFC_EVT_TRANSACTION_AID_TAG )return -EPROTO ; - transaction =devm_kzalloc (dev, skb->len -2 ,GFP_KERNEL ); -if (!transaction) -return -ENOMEM ; - - transaction->aid_len = skb->data[1 ]; + aid_len = skb->data[1 ]; -/* Checking if the length of the AID is valid */ -if (transaction->aid_len >sizeof (transaction->aid)) { -devm_kfree (dev, transaction); -return -EINVAL ; - } +if (skb->len < aid_len +4 || aid_len >sizeof (transaction->aid)) +return -EPROTO ; -memcpy (transaction->aid, &skb->data[2 ], - transaction->aid_len); + params_len = skb->data[aid_len +3 ]; -/* Check next byte is PARAMETERS tag (82) */ -if (skb->data[transaction->aid_len +2 ] != -NFC_EVT_TRANSACTION_PARAMS_TAG ) { -devm_kfree (dev, transaction); +/* Verify PARAMETERS tag is (82), and final check that there is enough +* space in the packet to read everything. +*/ +if ((skb->data[aid_len +2 ] !=NFC_EVT_TRANSACTION_PARAMS_TAG ) || + (skb->len < aid_len +4 + params_len))return -EPROTO ; - } - transaction->params_len = skb->data[transaction->aid_len +3 ]; + transaction =devm_kzalloc (dev,sizeof (*transaction) + params_len,GFP_KERNEL ); +if (!transaction) +return -ENOMEM ; -/* Total size is allocated (skb->len - 2) minus fixed array members */ -if (transaction->params_len > ((skb->len -2 ) - -sizeof (struct nfc_evt_transaction ))) { -devm_kfree (dev, transaction); -return -EINVAL ; - } + transaction->aid_len = aid_len; + transaction->params_len = params_len; -memcpy (transaction->params, skb->data + - transaction->aid_len +4 , transaction->params_len); +memcpy (transaction->aid, &skb->data[2 ], aid_len); +memcpy (transaction->params, &skb->data[aid_len +4 ], params_len); r =nfc_se_transaction (hdev->ndev, host, transaction);break ;
Signature · 1 line
2.36.1.255.ge46751e96f-goog