Skip to content

Commit

Permalink
fix: prevent UAF in NativeImage.getBitmap (#25782)
Browse files Browse the repository at this point in the history
  • Loading branch information
nornagon committed Oct 6, 2020
1 parent 0632d59 commit f31a1c9
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions shell/common/api/electron_api_native_image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ base::win::ScopedHICON ReadICOFromPath(int size, const base::FilePath& path) {
}
#endif

void Noop(char*, void*) {}

} // namespace

NativeImage::NativeImage(v8::Isolate* isolate, const gfx::Image& image)
Expand Down Expand Up @@ -224,6 +222,10 @@ std::string NativeImage::ToDataURL(gin::Arguments* args) {
image_.AsImageSkia().GetRepresentation(scale_factor).GetBitmap());
}

void SkUnref(char* data, void* hint) {
reinterpret_cast<SkRefCnt*>(hint)->unref();
}

v8::Local<v8::Value> NativeImage::GetBitmap(gin::Arguments* args) {
float scale_factor = GetScaleFactorFromOptions(args);

Expand All @@ -232,9 +234,10 @@ v8::Local<v8::Value> NativeImage::GetBitmap(gin::Arguments* args) {
SkPixelRef* ref = bitmap.pixelRef();
if (!ref)
return node::Buffer::New(args->isolate(), 0).ToLocalChecked();
ref->ref();
return node::Buffer::New(args->isolate(),
reinterpret_cast<char*>(ref->pixels()),
bitmap.computeByteSize(), &Noop, nullptr)
bitmap.computeByteSize(), &SkUnref, ref)
.ToLocalChecked();
}

Expand Down

0 comments on commit f31a1c9

Please sign in to comment.