[sanitize] Remove table-altering code parts

Part of fixing https://github.com/harfbuzz/harfbuzz/issues/5535

This makes two changes to previous behavior:
- If a FontFeatureSetting offset was wrong as per old Adobe
  behavior, we used to recover from. With this change (and immutable
  tables), we don't do that anymore,
- Similarly, if a cmap subtable has too-large of a length pointing
  outside of the cmap table, we used to recover from this, but
  don't anymore.
This commit is contained in:
Behdad Esfahbod committed 2025-11-09 15:28:19 -07:00
1 parent 83dc4d1d35
commit 665afc08f6
4 files changed
+7 -101

No files matched your search

+1 -8
View File
@@ -522,16 +522,9 @@ struct OffsetTo : Offset<OffsetType, has_null>
return_trace (sanitize_shallow (c, base) &&
hb_barrier () &&
(this->is_null () ||
c->dispatch (StructAtOffset<Type> (base, *this), std::forward<Ts> (ds)...) ||
neuter (c)));
c->dispatch (StructAtOffset<Type> (base, *this), std::forward<Ts> (ds)...)));
}
/* Set the offset to Null */
bool neuter (hb_sanitize_context_t *c) const
{
if (!has_null) return false;
return c->try_set (this, 0);
}
DEFINE_SIZE_STATIC (sizeof (OffsetType));
};
/* Partial specializations. */
+1 -10
View File
@@ -697,16 +697,7 @@ struct CmapSubtableFormat4
hb_barrier ();
if (unlikely (!c->check_range (this, length)))
{
/* Some broken fonts have too long of a "length" value.
* If that is the case, just change the value to truncate
* the subtable at the end of the blob. */
uint16_t new_length = (uint16_t) hb_min ((uintptr_t) 65535,
(uintptr_t) (c->end -
(char *) this));
if (!c->try_set (&length, new_length))
return_trace (false);
}
return_trace (false);
return_trace (16 + 4 * (unsigned int) segCountX2 <= length);
}
+2 -46
View File
@@ -828,46 +828,7 @@ struct Feature
const Record_sanitize_closure_t *closure = nullptr) const
{
TRACE_SANITIZE (this);
if (unlikely (!(c->check_struct (this) && lookupIndex.sanitize (c))))
return_trace (false);
hb_barrier ();
/* Some earlier versions of Adobe tools calculated the offset of the
* FeatureParams subtable from the beginning of the FeatureList table!
*
* If sanitizing "failed" for the FeatureParams subtable, try it with the
* alternative location. We would know sanitize "failed" if old value
* of the offset was non-zero, but it's zeroed now.
*
* Only do this for the 'size' feature, since at the time of the faulty
* Adobe tools, only the 'size' feature had FeatureParams defined.
*/
if (likely (featureParams.is_null ()))
return_trace (true);
unsigned int orig_offset = featureParams;
if (unlikely (!featureParams.sanitize (c, this, closure ? closure->tag : HB_TAG_NONE)))
return_trace (false);
hb_barrier ();
if (featureParams == 0 && closure &&
closure->tag == HB_TAG ('s','i','z','e') &&
closure->list_base && closure->list_base < this)
{
unsigned int new_offset_int = orig_offset -
(((char *) this) - ((char *) closure->list_base));
Offset16To<FeatureParams> new_offset;
/* Check that it would not overflow. */
new_offset = new_offset_int;
if (new_offset == new_offset_int &&
c->try_set (&featureParams, new_offset_int) &&
!featureParams.sanitize (c, this, closure ? closure->tag : HB_TAG_NONE))
return_trace (false);
}
return_trace (true);
return_trace (c->check_struct (this) && lookupIndex.sanitize (c));
}
Offset16To<FeatureParams>
@@ -1433,7 +1394,7 @@ struct Lookup
if (unlikely (!get_subtables<TSubTable> ().sanitize (c, this, get_type ())))
return_trace (false);
if (unlikely (get_type () == TSubTable::Extension && !c->get_edit_count ()))
if (unlikely (get_type () == TSubTable::Extension))
{
hb_barrier ();
@@ -1441,11 +1402,6 @@ struct Lookup
* have the same type, which shall not be the Extension type
* itself (but we already checked for that).
* This is specially important if one has a reverse type!
*
* We only do this if sanitizer edit_count is zero. Otherwise,
* some of the subtables might have become insane after they
* were sanity-checked by the edits of subsequent subtables.
* https://bugs.chromium.org/p/chromium/issues/detail?id=960331
*/
unsigned int type = get_subtable<TSubTable> (0).u.extension.get_type ();
for (unsigned int i = 1; i < subtables; i++)
+3 -37
View File
@@ -125,7 +125,7 @@ struct hb_sanitize_context_t :
length (0),
max_ops (0), max_subtables (0),
recursion_depth (0),
writable (false), edit_count (0),
writable (false),
blob (nullptr),
num_glyphs (65536),
num_glyphs_set (false),
@@ -236,7 +236,6 @@ struct hb_sanitize_context_t :
this->max_ops = hb_clamp (m,
(unsigned) HB_SANITIZE_MAX_OPS_MIN,
(unsigned) HB_SANITIZE_MAX_OPS_MAX);
this->edit_count = 0;
this->debug_depth = 0;
this->recursion_depth = 0;
@@ -249,8 +248,8 @@ struct hb_sanitize_context_t :
void end_processing ()
{
DEBUG_MSG_LEVEL (SANITIZE, this->start, 0, -1,
"end [%p..%p] %u edit requests",
this->start, this->end, this->edit_count);
"end [%p..%p]",
this->start, this->end);
hb_blob_destroy (this->blob);
this->blob = nullptr;
@@ -258,9 +257,6 @@ struct hb_sanitize_context_t :
this->length = 0;
}
unsigned get_edit_count () { return edit_count; }
bool check_ops(unsigned count)
{
/* Avoid underflow */
@@ -404,35 +400,6 @@ struct hb_sanitize_context_t :
return likely (this->check_point ((const char *) obj + obj->min_size));
}
bool may_edit (const void *base, unsigned int len)
{
if (this->edit_count >= HB_SANITIZE_MAX_EDITS)
return false;
const char *p = (const char *) base;
this->edit_count++;
DEBUG_MSG_LEVEL (SANITIZE, p, this->debug_depth+1, 0,
"may_edit(%u) [%p..%p] (%u bytes) in [%p..%p] -> %s",
this->edit_count,
p, p + len, len,
this->start, this->end,
this->writable ? "GRANTED" : "DENIED");
return this->writable;
}
template <typename Type, typename ValueType>
bool try_set (const Type *obj, const ValueType &v)
{
if (this->may_edit (obj, hb_static_size (Type)))
{
* const_cast<Type *> (obj) = v;
return true;
}
return false;
}
template <typename Type>
hb_blob_t *sanitize_blob (hb_blob_t *blob)
{
@@ -483,7 +450,6 @@ struct hb_sanitize_context_t :
private:
int recursion_depth;
bool writable;
unsigned int edit_count;
hb_blob_t *blob;
unsigned int num_glyphs;
bool num_glyphs_set;