@@ -1421,6 +1421,8 @@ static int copy_reference_state(struct bpf_verifier_state *dst, const struct bpf
1421
1421
dst->active_preempt_locks = src->active_preempt_locks;
1422
1422
dst->active_rcu_lock = src->active_rcu_lock;
1423
1423
dst->active_irq_id = src->active_irq_id;
1424
+ dst->active_lock_id = src->active_lock_id;
1425
+ dst->active_lock_ptr = src->active_lock_ptr;
1424
1426
return 0;
1425
1427
}
1426
1428
@@ -1520,6 +1522,8 @@ static int acquire_lock_state(struct bpf_verifier_env *env, int insn_idx, enum r
1520
1522
s->ptr = ptr;
1521
1523
1522
1524
state->active_locks++;
1525
+ state->active_lock_id = id;
1526
+ state->active_lock_ptr = ptr;
1523
1527
return 0;
1524
1528
}
1525
1529
@@ -1570,16 +1574,24 @@ static bool find_reference_state(struct bpf_verifier_state *state, int ptr_id)
1570
1574
1571
1575
static int release_lock_state(struct bpf_verifier_state *state, int type, int id, void *ptr)
1572
1576
{
1577
+ void *prev_ptr = NULL;
1578
+ u32 prev_id = 0;
1573
1579
int i;
1574
1580
1575
1581
for (i = 0; i < state->acquired_refs; i++) {
1576
- if (state->refs[i].type != type)
1577
- continue;
1578
- if (state->refs[i].id == id && state->refs[i].ptr == ptr) {
1582
+ if (state->refs[i].type == type && state->refs[i].id == id &&
1583
+ state->refs[i].ptr == ptr) {
1579
1584
release_reference_state(state, i);
1580
1585
state->active_locks--;
1586
+ /* Reassign active lock (id, ptr). */
1587
+ state->active_lock_id = prev_id;
1588
+ state->active_lock_ptr = prev_ptr;
1581
1589
return 0;
1582
1590
}
1591
+ if (state->refs[i].type & REF_TYPE_LOCK_MASK) {
1592
+ prev_id = state->refs[i].id;
1593
+ prev_ptr = state->refs[i].ptr;
1594
+ }
1583
1595
}
1584
1596
return -EINVAL;
1585
1597
}
@@ -8283,6 +8295,14 @@ static int process_spin_lock(struct bpf_verifier_env *env, int regno, int flags)
8283
8295
type = REF_TYPE_RES_LOCK;
8284
8296
else
8285
8297
type = REF_TYPE_LOCK;
8298
+ if (!find_lock_state(cur, type, reg->id, ptr)) {
8299
+ verbose(env, "%s_unlock of different lock\n", lock_str);
8300
+ return -EINVAL;
8301
+ }
8302
+ if (reg->id != cur->active_lock_id || ptr != cur->active_lock_ptr) {
8303
+ verbose(env, "%s_unlock cannot be out of order\n", lock_str);
8304
+ return -EINVAL;
8305
+ }
8286
8306
if (release_lock_state(cur, type, reg->id, ptr)) {
8287
8307
verbose(env, "%s_unlock of different lock\n", lock_str);
8288
8308
return -EINVAL;
@@ -12475,8 +12495,7 @@ static int check_reg_allocation_locked(struct bpf_verifier_env *env, struct bpf_
12475
12495
12476
12496
if (!env->cur_state->active_locks)
12477
12497
return -EINVAL;
12478
- s = find_lock_state(env->cur_state, REF_TYPE_LOCK | REF_TYPE_RES_LOCK | REF_TYPE_RES_LOCK_IRQ,
12479
- id, ptr);
12498
+ s = find_lock_state(env->cur_state, REF_TYPE_LOCK_MASK, id, ptr);
12480
12499
if (!s) {
12481
12500
verbose(env, "held lock and object are not in the same allocation\n");
12482
12501
return -EINVAL;
@@ -18531,6 +18550,10 @@ static bool refsafe(struct bpf_verifier_state *old, struct bpf_verifier_state *c
18531
18550
if (!check_ids(old->active_irq_id, cur->active_irq_id, idmap))
18532
18551
return false;
18533
18552
18553
+ if (!check_ids(old->active_lock_id, cur->active_lock_id, idmap) ||
18554
+ old->active_lock_ptr != cur->active_lock_ptr)
18555
+ return false;
18556
+
18534
18557
for (i = 0; i < old->acquired_refs; i++) {
18535
18558
if (!check_ids(old->refs[i].id, cur->refs[i].id, idmap) ||
18536
18559
old->refs[i].type != cur->refs[i].type)
0 commit comments