Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

c-api: fix use-after-free in C++ copy assignment operators (#15085)

netliomax25-code89ae0bac1cbc
diff --git a/crates/c-api/include/wasmtime/_val_class.hh b/crates/c-api/include/wasmtime/_val_class.hh
index 271079a14c..c3f165fce9 100644
--- a/crates/c-api/include/wasmtime/_val_class.hh
+++ b/crates/c-api/include/wasmtime/_val_class.hh
@@ -117,11 +117,7 @@ public:
Val(const Val &other) { wasmtime_val_clone(&other.val, &val); }
/// Copy assignment to clone from `other`.
- Val &operator=(const Val &other) {
- wasmtime_val_unroot(&val);
- wasmtime_val_clone(&other.val, &val);
- return *this;
- }
+ Val &operator=(const Val &other) { return *this = Val(other); }
/// Move constructor to move the contents of `other`.
Val(Val &&other) {
diff --git a/crates/c-api/include/wasmtime/component/types/component.hh b/crates/c-api/include/wasmtime/component/types/component.hh
index c3a7bab914..026981813f 100644
--- a/crates/c-api/include/wasmtime/component/types/component.hh
+++ b/crates/c-api/include/wasmtime/component/types/component.hh
@@ -95,9 +95,7 @@ public:
/// Copies another item into this one.
ComponentItem &operator=(const ComponentItem &other) {
- wasmtime_component_item_delete(&item);
- wasmtime_component_item_clone(&other.item, &item);
- return *this;
+ return *this = ComponentItem(other);
}
/// Moves another item into this one.
diff --git a/crates/c-api/include/wasmtime/component/types/val.hh b/crates/c-api/include/wasmtime/component/types/val.hh
index 6a93b3de5c..464d9846ea 100644
--- a/crates/c-api/include/wasmtime/component/types/val.hh
+++ b/crates/c-api/include/wasmtime/component/types/val.hh
@@ -216,11 +216,7 @@ public:
}
/// Copies another type into this one.
- ValType &operator=(const ValType &other) {
- wasmtime_component_valtype_delete(&ty);
- wasmtime_component_valtype_clone(&other.ty, &ty);
- return *this;
- }
+ ValType &operator=(const ValType &other) { return *this = ValType(other); }
/// Moves another type into this one.
ValType(ValType &&other) : ty(other.ty) {
diff --git a/crates/c-api/include/wasmtime/component/val.hh b/crates/c-api/include/wasmtime/component/val.hh
index 93a55e7689..31142e9653 100644
--- a/crates/c-api/include/wasmtime/component/val.hh
+++ b/crates/c-api/include/wasmtime/component/val.hh
@@ -84,8 +84,12 @@ public: \
* \brief Copy assignment to clone from `other`. \
*/ \
name &operator=(const name &other) { \
+ /* `other` may be `*this` or owned by it, so clone it before destroying */ \
+ Raw old = raw; \
+ copy(this == &other ? old : other.raw); \
+ std::swap(old, raw); \
destroy(); \
- copy(other.raw); \
+ raw = old; \
return *this; \
} \
\
diff --git a/crates/c-api/include/wasmtime/helpers.hh b/crates/c-api/include/wasmtime/helpers.hh
index 5d1ddf135a..579ffb513f 100644
--- a/crates/c-api/include/wasmtime/helpers.hh
+++ b/crates/c-api/include/wasmtime/helpers.hh
@@ -108,11 +108,7 @@ public: \
explicit name(Raw raw) : raw(raw) {} \
name(const name &other) { capi_type##_clone(&other.raw, &raw); } \
\
- name &operator=(const name &other) { \
- capi_type##_unroot(&raw); \
- capi_type##_clone(&other.raw, &raw); \
- return *this; \
- } \
+ name &operator=(const name &other) { return *this = name(other); } \
name(name &&other) { \
raw = other.raw; \
capi_type##_set_null(&other.raw); \
diff --git a/crates/c-api/include/wasmtime/types/_structref_class.hh b/crates/c-api/include/wasmtime/types/_structref_class.hh
index e56e9cb1da..26a3ab469e 100644
--- a/crates/c-api/include/wasmtime/types/_structref_class.hh
+++ b/crates/c-api/include/wasmtime/types/_structref_class.hh
@@ -27,9 +27,7 @@ public:
}
/// Copy assignment operator.
StorageType &operator=(const StorageType &other) {
- wasmtime_storage_type_delete(&ty);
- wasmtime_storage_type_clone(&other.ty, &ty);
- return *this;
+ return *this = StorageType(other);
}
~StorageType() { wasmtime_storage_type_delete(&ty); }
/// Move constructor
@@ -91,9 +89,7 @@ public:
}
/// Copy assignment operator.
FieldType &operator=(const FieldType &other) {
- wasmtime_field_type_delete(&ty);
- wasmtime_field_type_clone(&other.ty, &ty);
- return *this;
+ return *this = FieldType(other);
}
~FieldType() { wasmtime_field_type_delete(&ty); }
/// Move constructor
diff --git a/crates/c-api/include/wasmtime/types/_val_class.hh b/crates/c-api/include/wasmtime/types/_val_class.hh
index bcc11e89c8..210b1409e3 100644
--- a/crates/c-api/include/wasmtime/types/_val_class.hh
+++ b/crates/c-api/include/wasmtime/types/_val_class.hh
@@ -93,8 +93,10 @@ public:
ValType &operator=(const ValType &other) {
ptr.reset(wasm_valtype_copy(other.ptr.get()));
ref = ptr.get();
+ wasmtime_valtype_t cloned;
+ wasmtime_valtype_clone(&other.wasmtime_ty, &cloned);
wasmtime_valtype_delete(&wasmtime_ty);
- wasmtime_valtype_clone(&other.wasmtime_ty, &wasmtime_ty);
+ wasmtime_ty = cloned;
return *this;
}
~ValType() { wasmtime_valtype_delete(&wasmtime_ty); }
diff --git a/crates/c-api/include/wasmtime/types/val.hh b/crates/c-api/include/wasmtime/types/val.hh
index 30d099e635..42ec8d684f 100644
--- a/crates/c-api/include/wasmtime/types/val.hh
+++ b/crates/c-api/include/wasmtime/types/val.hh
@@ -33,11 +33,7 @@ public:
/// Copy constructor.
HeapType(const HeapType &other) { wasmtime_heaptype_clone(&other.ty, &ty); }
/// Copy assignment operator.
- HeapType &operator=(const HeapType &other) {
- wasmtime_heaptype_delete(&ty);
- wasmtime_heaptype_clone(&other.ty, &ty);
- return *this;
- }
+ HeapType &operator=(const HeapType &other) { return *this = HeapType(other); }
~HeapType() {
if (is_concrete())
wasmtime_heaptype_delete(&ty);
@@ -219,11 +215,7 @@ public:
/// Copy constructor.
RefType(const RefType &other) { wasmtime_reftype_clone(&other.ty, &ty); }
/// Copy assignment operator.
- RefType &operator=(const RefType &other) {
- wasmtime_reftype_delete(&ty);
- wasmtime_reftype_clone(&other.ty, &ty);
- return *this;
- }
+ RefType &operator=(const RefType &other) { return *this = RefType(other); }
~RefType() { wasmtime_reftype_delete(&ty); }
/// Move constructor
RefType(RefType &&other) {
diff --git a/crates/c-api/tests/component/values.cc b/crates/c-api/tests/component/values.cc
index c7ae5459f8..3737fa381f 100644
--- a/crates/c-api/tests/component/values.cc
+++ b/crates/c-api/tests/component/values.cc
@@ -930,6 +930,24 @@ TEST(component, flags) {
EXPECT_EQ(f.size(), 3);
}
+TEST(component, assign_from_self_or_child) {
+ Val list = List({Val::string("hello"), uint32_t(2)});
+ const Val &list_alias = list;
+ list = list_alias;
+ ASSERT_TRUE(list.is_list());
+ EXPECT_EQ(list.get_list().size(), 2);
+
+ Val opt = WitOption(Val::string("hello"));
+ const Val &opt_alias = opt;
+ opt = opt_alias;
+ ASSERT_TRUE(opt.is_option());
+ EXPECT_EQ(opt.get_option().value()->get_string(), "hello");
+
+ opt = *opt.get_option().value();
+ ASSERT_TRUE(opt.is_string());
+ EXPECT_EQ(opt.get_string(), "hello");
+}
+
TEST(component, value_host_resource) {
static const uint32_t RESOURCE_TYPE = 100;