Uploaded image for project: 'Lustre'
  1. Lustre
  2. LU-21227

lod_mdt_alloc_specific() overflows stripes[]/idx_array[] when overstripe clamping shrinks the stripe count

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Medium Medium
    • None
    • None
    • None
    • 3
    • 9223372036854775807

      In lod_prep_md_striped_create(), when a create request has
      LMV_HASH_FLAG_OVERSTRIPED set and stripe_count exceeds mdt_count *
      lod_max_stripes_per_mdt, the code clamps the request down to that
      limit — but only in the local variable stripe_count:

      stripe_count = lo->ldo_dir_stripe_count;
      if (lo->ldo_dir_hash_type & LMV_HASH_FLAG_OVERSTRIPED) {
      if (stripe_count > mdt_count * lod->lod_max_stripes_per_mdt)

      { if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) stripe_count = mdt_count * lod->lod_max_stripes_per_mdt; else RETURN(-E2BIG); }

      ...
      }

      lo->ldo_dir_stripe_count itself is not updated at this point — it only
      gets set to the final value after allocation succeeds. stripes[] and
      idx_array[] are then allocated/sized using the clamped stripe_count:

      OBD_ALLOC_PTR_ARRAY(stripes, stripe_count);
      ...
      OBD_ALLOC_PTR_ARRAY(idx_array, stripe_count);

      When lo->ldo_dir_stripe_offset != LMV_OFFSET_DEFAULT (an
      explicit/specific MDT offset was requested), allocation goes through
      lod_mdt_alloc_specific(). Unlike lod_mdt_alloc_qos() and
      lod_mdt_alloc_rr(), which take the stripe count as an explicit
      parameter, lod_mdt_alloc_specific() reads the loop bound from
      lo->ldo_dir_stripe_count directly — the stale, pre-clamp value.

      Result: whenever overstripe clamping actually shrinks the stripe count
      (e.g. requested 8, clamped to 5) and the offset is non-default,
      lod_mdt_alloc_specific() iterates up to the stale larger count (8)
      while stripes[]/idx_array[] were only allocated for the clamped count
      (5) — a heap buffer overflow writing past the end of both arrays.

      This was caught directly by KASAN while writing a new KUnit test
      (lod_prep_md_striped_create_test.c) that exercised this exact
      combination (overstripe clamp + specific offset):

      BUG: KASAN: slab-out-of-bounds in lod_mdt_alloc_specific
      ...
      kasan: bad access detected

      Fix direction: have lod_mdt_alloc_specific() take the
      (already-clamped) stripe count as a parameter, the same way
      lod_mdt_alloc_qos()/lod_mdt_alloc_rr() do, instead of re-reading
      lo->ldo_dir_stripe_count. Alternatively, update
      lo->ldo_dir_stripe_count to the clamped value immediately after
      clamping, before any allocator is called — but passing it explicitly
      is more consistent with the other two allocators and avoids relying on
      object state being correct mid-function.

            rsahlberg@whamcloud.com Ronnie Sahlberg
            rsahlberg@whamcloud.com Ronnie Sahlberg
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: