Skip to content

Commit 9423973

Browse files
Jakob-Koscheltiwai
authored andcommitted
firewire: remove check of list iterator against head past the loop body
When list_for_each_entry() completes the iteration over the whole list without breaking the loop, the iterator value will be a bogus pointer computed based on the head element. While it is safe to use the pointer to determine if it was computed based on the head element, either with list_entry_is_head() or &pos->member == head, using the iterator variable after the loop should be avoided. In preparation to limit the scope of a list iterator to the list traversal loop, use a dedicated pointer to point to the found element [1]. Link: https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/ [1] Cc: <stable@vger.kernel.org> Signed-off-by: Jakob Koschel <jakobkoschel@gmail.com> Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp> Link: https://lore.kernel.org/r/20220409041243.603210-3-o-takashi@sakamocchi.jp Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent b7c81f8 commit 9423973

2 files changed

Lines changed: 23 additions & 20 deletions

File tree

drivers/firewire/core-transaction.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,25 @@ static int try_cancel_split_timeout(struct fw_transaction *t)
7373
static int close_transaction(struct fw_transaction *transaction,
7474
struct fw_card *card, int rcode)
7575
{
76-
struct fw_transaction *t;
76+
struct fw_transaction *t = NULL, *iter;
7777
unsigned long flags;
7878

7979
spin_lock_irqsave(&card->lock, flags);
80-
list_for_each_entry(t, &card->transaction_list, link) {
81-
if (t == transaction) {
82-
if (!try_cancel_split_timeout(t)) {
80+
list_for_each_entry(iter, &card->transaction_list, link) {
81+
if (iter == transaction) {
82+
if (!try_cancel_split_timeout(iter)) {
8383
spin_unlock_irqrestore(&card->lock, flags);
8484
goto timed_out;
8585
}
86-
list_del_init(&t->link);
87-
card->tlabel_mask &= ~(1ULL << t->tlabel);
86+
list_del_init(&iter->link);
87+
card->tlabel_mask &= ~(1ULL << iter->tlabel);
88+
t = iter;
8889
break;
8990
}
9091
}
9192
spin_unlock_irqrestore(&card->lock, flags);
9293

93-
if (&t->link != &card->transaction_list) {
94+
if (t) {
9495
t->callback(card, rcode, NULL, 0, t->callback_data);
9596
return 0;
9697
}
@@ -935,7 +936,7 @@ EXPORT_SYMBOL(fw_core_handle_request);
935936

936937
void fw_core_handle_response(struct fw_card *card, struct fw_packet *p)
937938
{
938-
struct fw_transaction *t;
939+
struct fw_transaction *t = NULL, *iter;
939940
unsigned long flags;
940941
u32 *data;
941942
size_t data_length;
@@ -947,20 +948,21 @@ void fw_core_handle_response(struct fw_card *card, struct fw_packet *p)
947948
rcode = HEADER_GET_RCODE(p->header[1]);
948949

949950
spin_lock_irqsave(&card->lock, flags);
950-
list_for_each_entry(t, &card->transaction_list, link) {
951-
if (t->node_id == source && t->tlabel == tlabel) {
952-
if (!try_cancel_split_timeout(t)) {
951+
list_for_each_entry(iter, &card->transaction_list, link) {
952+
if (iter->node_id == source && iter->tlabel == tlabel) {
953+
if (!try_cancel_split_timeout(iter)) {
953954
spin_unlock_irqrestore(&card->lock, flags);
954955
goto timed_out;
955956
}
956-
list_del_init(&t->link);
957-
card->tlabel_mask &= ~(1ULL << t->tlabel);
957+
list_del_init(&iter->link);
958+
card->tlabel_mask &= ~(1ULL << iter->tlabel);
959+
t = iter;
958960
break;
959961
}
960962
}
961963
spin_unlock_irqrestore(&card->lock, flags);
962964

963-
if (&t->link == &card->transaction_list) {
965+
if (!t) {
964966
timed_out:
965967
fw_notice(card, "unsolicited response (source %x, tlabel %x)\n",
966968
source, tlabel);

drivers/firewire/sbp2.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ static void sbp2_status_write(struct fw_card *card, struct fw_request *request,
408408
void *payload, size_t length, void *callback_data)
409409
{
410410
struct sbp2_logical_unit *lu = callback_data;
411-
struct sbp2_orb *orb;
411+
struct sbp2_orb *orb = NULL, *iter;
412412
struct sbp2_status status;
413413
unsigned long flags;
414414

@@ -433,17 +433,18 @@ static void sbp2_status_write(struct fw_card *card, struct fw_request *request,
433433

434434
/* Lookup the orb corresponding to this status write. */
435435
spin_lock_irqsave(&lu->tgt->lock, flags);
436-
list_for_each_entry(orb, &lu->orb_list, link) {
436+
list_for_each_entry(iter, &lu->orb_list, link) {
437437
if (STATUS_GET_ORB_HIGH(status) == 0 &&
438-
STATUS_GET_ORB_LOW(status) == orb->request_bus) {
439-
orb->rcode = RCODE_COMPLETE;
440-
list_del(&orb->link);
438+
STATUS_GET_ORB_LOW(status) == iter->request_bus) {
439+
iter->rcode = RCODE_COMPLETE;
440+
list_del(&iter->link);
441+
orb = iter;
441442
break;
442443
}
443444
}
444445
spin_unlock_irqrestore(&lu->tgt->lock, flags);
445446

446-
if (&orb->link != &lu->orb_list) {
447+
if (orb) {
447448
orb->callback(orb, &status);
448449
kref_put(&orb->kref, free_orb); /* orb callback reference */
449450
} else {

0 commit comments

Comments
 (0)