Skip to content

Commit 6ebebac

Browse files
Justin T. Gibbsbehlendorf
authored andcommitted
Illumos 5531 - NULL pointer dereference in dsl_prop_get_ds()
5531 NULL pointer dereference in dsl_prop_get_ds() Author: Justin T. Gibbs <justing@spectralogic.com> Reviewed by: Matthew Ahrens <mahrens@delphix.com> Reviewed by: Dan McDonald <danmcd@omniti.com> Reviewed by: George Wilson <george@delphix.com> Reviewed by: Bayard Bell <buffer.g.overflow@gmail.com> Approved by: Robert Mustacchi <rm@joyent.com> References: https://www.illumos.org/issues/5531 illumos/illumos-gate@e57a022 Ported-by: Chris Dunlop <chris@onthe.net.au> Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
1 parent 0c66c32 commit 6ebebac

File tree

7 files changed

+115
-15
lines changed

7 files changed

+115
-15
lines changed

include/sys/dbuf.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,15 @@ int dbuf_hold_impl(struct dnode *dn, uint8_t level, uint64_t blkid, int create,
262262
void dbuf_prefetch(struct dnode *dn, uint64_t blkid, zio_priority_t prio);
263263

264264
void dbuf_add_ref(dmu_buf_impl_t *db, void *tag);
265+
boolean_t dbuf_try_add_ref(dmu_buf_t *db, objset_t *os, uint64_t obj,
266+
uint64_t blkid, void *tag);
265267
uint64_t dbuf_refcount(dmu_buf_impl_t *db);
266268

267269
void dbuf_rele(dmu_buf_impl_t *db, void *tag);
268270
void dbuf_rele_and_unlock(dmu_buf_impl_t *db, void *tag);
269271

270-
dmu_buf_impl_t *dbuf_find(struct dnode *dn, uint8_t level, uint64_t blkid);
272+
dmu_buf_impl_t *dbuf_find(struct objset *os, uint64_t object, uint8_t level,
273+
uint64_t blkid);
271274

272275
int dbuf_read(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags);
273276
void dmu_buf_will_not_fill(dmu_buf_t *db, dmu_tx_t *tx);

include/sys/dmu.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,23 @@ int dmu_spill_hold_existing(dmu_buf_t *bonus, void *tag, dmu_buf_t **dbp);
454454
*/
455455
int dmu_buf_hold(objset_t *os, uint64_t object, uint64_t offset,
456456
void *tag, dmu_buf_t **, int flags);
457+
458+
/*
459+
* Add a reference to a dmu buffer that has already been held via
460+
* dmu_buf_hold() in the current context.
461+
*/
457462
void dmu_buf_add_ref(dmu_buf_t *db, void* tag);
463+
464+
/*
465+
* Attempt to add a reference to a dmu buffer that is in an unknown state,
466+
* using a pointer that may have been invalidated by eviction processing.
467+
* The request will succeed if the passed in dbuf still represents the
468+
* same os/object/blkid, is ineligible for eviction, and has at least
469+
* one hold by a user other than the syncer.
470+
*/
471+
boolean_t dmu_buf_try_add_ref(dmu_buf_t *, objset_t *os, uint64_t object,
472+
uint64_t blkid, void *tag);
473+
458474
void dmu_buf_rele(dmu_buf_t *db, void *tag);
459475
uint64_t dmu_buf_refcount(dmu_buf_t *db);
460476

include/sys/dsl_dataset.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ dsl_dataset_phys(dsl_dataset_t *ds)
197197

198198
int dsl_dataset_hold(struct dsl_pool *dp, const char *name, void *tag,
199199
dsl_dataset_t **dsp);
200+
boolean_t dsl_dataset_try_add_ref(struct dsl_pool *dp, dsl_dataset_t *ds,
201+
void *tag);
200202
int dsl_dataset_hold_obj(struct dsl_pool *dp, uint64_t dsobj, void *tag,
201203
dsl_dataset_t **);
202204
void dsl_dataset_rele(dsl_dataset_t *ds, void *tag);

module/zfs/dbuf.c

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,13 @@ dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
149149
(dbuf)->db_blkid == (blkid))
150150

151151
dmu_buf_impl_t *
152-
dbuf_find(dnode_t *dn, uint8_t level, uint64_t blkid)
152+
dbuf_find(objset_t *os, uint64_t obj, uint8_t level, uint64_t blkid)
153153
{
154154
dbuf_hash_table_t *h = &dbuf_hash_table;
155-
objset_t *os = dn->dn_objset;
156-
uint64_t obj;
157155
uint64_t hv;
158156
uint64_t idx;
159157
dmu_buf_impl_t *db;
160158

161-
obj = dn->dn_object;
162159
hv = DBUF_HASH(os, obj, level, blkid);
163160
idx = hv & h->hash_table_mask;
164161

@@ -177,6 +174,24 @@ dbuf_find(dnode_t *dn, uint8_t level, uint64_t blkid)
177174
return (NULL);
178175
}
179176

177+
static dmu_buf_impl_t *
178+
dbuf_find_bonus(objset_t *os, uint64_t object)
179+
{
180+
dnode_t *dn;
181+
dmu_buf_impl_t *db = NULL;
182+
183+
if (dnode_hold(os, object, FTAG, &dn) == 0) {
184+
rw_enter(&dn->dn_struct_rwlock, RW_READER);
185+
if (dn->dn_bonus != NULL) {
186+
db = dn->dn_bonus;
187+
mutex_enter(&db->db_mtx);
188+
}
189+
rw_exit(&dn->dn_struct_rwlock);
190+
dnode_rele(dn, FTAG);
191+
}
192+
return (db);
193+
}
194+
180195
/*
181196
* Insert an entry into the hash table. If there is already an element
182197
* equal to elem in the hash table, then the already existing element
@@ -2000,7 +2015,7 @@ dbuf_prefetch(dnode_t *dn, uint64_t blkid, zio_priority_t prio)
20002015
return;
20012016

20022017
/* dbuf_find() returns with db_mtx held */
2003-
if ((db = dbuf_find(dn, 0, blkid))) {
2018+
if ((db = dbuf_find(dn->dn_objset, dn->dn_object, 0, blkid))) {
20042019
/*
20052020
* This dbuf is already in the cache. We assume that
20062021
* it is already CACHED, or else about to be either
@@ -2048,7 +2063,8 @@ __dbuf_hold_impl(struct dbuf_hold_impl_data *dh)
20482063
*(dh->dh_dbp) = NULL;
20492064
top:
20502065
/* dbuf_find() returns with db_mtx held */
2051-
dh->dh_db = dbuf_find(dh->dh_dn, dh->dh_level, dh->dh_blkid);
2066+
dh->dh_db = dbuf_find(dh->dh_dn->dn_objset, dh->dh_dn->dn_object,
2067+
dh->dh_level, dh->dh_blkid);
20522068

20532069
if (dh->dh_db == NULL) {
20542070
dh->dh_bp = NULL;
@@ -2228,6 +2244,30 @@ dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
22282244
VERIFY(refcount_add(&db->db_holds, tag) > 1);
22292245
}
22302246

2247+
#pragma weak dmu_buf_try_add_ref = dbuf_try_add_ref
2248+
boolean_t
2249+
dbuf_try_add_ref(dmu_buf_t *db_fake, objset_t *os, uint64_t obj, uint64_t blkid,
2250+
void *tag)
2251+
{
2252+
dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
2253+
dmu_buf_impl_t *found_db;
2254+
boolean_t result = B_FALSE;
2255+
2256+
if (db->db_blkid == DMU_BONUS_BLKID)
2257+
found_db = dbuf_find_bonus(os, obj);
2258+
else
2259+
found_db = dbuf_find(os, obj, 0, blkid);
2260+
2261+
if (found_db != NULL) {
2262+
if (db == found_db && dbuf_refcount(db) > db->db_dirtycnt) {
2263+
(void) refcount_add(&db->db_holds, tag);
2264+
result = B_TRUE;
2265+
}
2266+
mutex_exit(&db->db_mtx);
2267+
}
2268+
return (result);
2269+
}
2270+
22312271
/*
22322272
* If you call dbuf_rele() you had better not be referencing the dnode handle
22332273
* unless you have some other direct or indirect hold on the dnode. (An indirect

module/zfs/dnode_sync.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx)
7777

7878
/* set dbuf's parent pointers to new indirect buf */
7979
for (i = 0; i < nblkptr; i++) {
80-
dmu_buf_impl_t *child = dbuf_find(dn, old_toplvl, i);
80+
dmu_buf_impl_t *child =
81+
dbuf_find(dn->dn_objset, dn->dn_object, old_toplvl, i);
8182

8283
if (child == NULL)
8384
continue;

module/zfs/dsl_dataset.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,13 @@ dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx,
351351
return (err);
352352
}
353353

354+
boolean_t
355+
dsl_dataset_try_add_ref(dsl_pool_t *dp, dsl_dataset_t *ds, void *tag)
356+
{
357+
return (dmu_buf_try_add_ref(ds->ds_dbuf, dp->dp_meta_objset,
358+
ds->ds_object, DMU_BONUS_BLKID, tag));
359+
}
360+
354361
int
355362
dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
356363
dsl_dataset_t **dsp)

module/zfs/dsl_prop.c

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,31 @@ dsl_prop_notify_all_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
441441
cbr = list_next(&dd->dd_prop_cbs, cbr)) {
442442
uint64_t value;
443443

444+
/*
445+
* Callback entries do not have holds on their datasets
446+
* so that datasets with registered callbacks are still
447+
* eligible for eviction. Unlike operations on callbacks
448+
* for a single dataset, we are performing a recursive
449+
* descent of related datasets and the calling context
450+
* for this iteration only has a dataset hold on the root.
451+
* Without a hold, the callback's pointer to the dataset
452+
* could be invalidated by eviction at any time.
453+
*
454+
* Use dsl_dataset_try_add_ref() to verify that the
455+
* dataset has not begun eviction processing and to
456+
* prevent eviction from occurring for the duration
457+
* of the callback. If the hold attempt fails, this
458+
* object is already being evicted and the callback can
459+
* be safely ignored.
460+
*/
461+
if (!dsl_dataset_try_add_ref(dp, cbr->cbr_ds, FTAG))
462+
continue;
463+
444464
if (dsl_prop_get_ds(cbr->cbr_ds, cbr->cbr_propname,
445465
sizeof (value), 1, &value, NULL) == 0)
446466
cbr->cbr_func(cbr->cbr_arg, value);
467+
468+
dsl_dataset_rele(cbr->cbr_ds, FTAG);
447469
}
448470
mutex_exit(&dd->dd_lock);
449471

@@ -496,19 +518,28 @@ dsl_prop_changed_notify(dsl_pool_t *dp, uint64_t ddobj,
496518
mutex_enter(&dd->dd_lock);
497519
for (cbr = list_head(&dd->dd_prop_cbs); cbr;
498520
cbr = list_next(&dd->dd_prop_cbs, cbr)) {
499-
uint64_t propobj = dsl_dataset_phys(cbr->cbr_ds)->ds_props_obj;
521+
uint64_t propobj;
500522

501-
if (strcmp(cbr->cbr_propname, propname) != 0)
523+
/*
524+
* cbr->cbf_ds may be invalidated due to eviction,
525+
* requiring the use of dsl_dataset_try_add_ref().
526+
* See comment block in dsl_prop_notify_all_cb()
527+
* for details.
528+
*/
529+
if (strcmp(cbr->cbr_propname, propname) != 0 ||
530+
!dsl_dataset_try_add_ref(dp, cbr->cbr_ds, FTAG))
502531
continue;
503532

533+
propobj = dsl_dataset_phys(cbr->cbr_ds)->ds_props_obj;
534+
504535
/*
505-
* If the property is set on this ds, then it is not
506-
* inherited here; don't call the callback.
536+
* If the property is not set on this ds, then it is
537+
* inherited here; call the callback.
507538
*/
508-
if (propobj && 0 == zap_contains(mos, propobj, propname))
509-
continue;
539+
if (propobj == 0 || zap_contains(mos, propobj, propname) != 0)
540+
cbr->cbr_func(cbr->cbr_arg, value);
510541

511-
cbr->cbr_func(cbr->cbr_arg, value);
542+
dsl_dataset_rele(cbr->cbr_ds, FTAG);
512543
}
513544
mutex_exit(&dd->dd_lock);
514545

0 commit comments

Comments
 (0)