GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — buffer: fix segfaults when `this` is not a Buffer. by monsanto · Pull Request #1486 · nodejs/node

Conversation

@monsanto

@monsanto monsanto commented Apr 21, 2015

Copy link
Copy Markdown
Contributor

Return Object.prototype.toString(this) in such a case; this approach is inspired by Array.prototype.toString.

Fixes #1485.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mscdex

mscdex commented Apr 21, 2015

Copy link
Copy Markdown
Contributor

Won't this technically be a problem for any of the prototype methods, not just toString()?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mscdex mscdex added the buffer Issues and PRs related to the buffer subsystem. label Apr 21, 2015
@JacksonTian

JacksonTian commented Apr 21, 2015

Copy link
Copy Markdown
Contributor
> Array.prototype.toString()
''
> Object.prototype.toString()
'[object Object]'
> Date.prototype.toString()
'Invalid Date'
> RegExp.prototype.toString()
'/(?:)/'
> ArrayBuffer.prototype.toString()
'[object Object]'

If don't overwrite toString in prototype chain, the Object.prototype.toString will be used. Maybe just output "" is ok.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@monsanto

monsanto commented Apr 21, 2015

Copy link
Copy Markdown
Contributor Author

@mscdex I would consider Buffer.prototype.toString a special case because it can be called implicitly, and should probably not throw if called.

But, yes, it's a problem, none of the C++ functions typecheck this. Also some of the typechecks that are already in node_buffer.cc are bugged out--for example, you can segfault by calling Buffer#copy w/ null or undefined as the first argument. I've fixed that case locally, I'm working on checking this on everything else.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@monsanto

monsanto commented Apr 21, 2015

Copy link
Copy Markdown
Contributor Author

I think that's all of them!

It is not safe to check HasIndexedPropertiesInExternalArrayData() on an arbitrary value you have coerced with ToObject or .As<Object>. It will cause a segfault on null and undefined. To this end, ARGS_THIS now requires a value, which it will check for being an object before checking the indexed properties. It implicitly assumes that there is an environment called env in scope, and as a consequence a few functions gained an env.

Also, there were a few instances where there was a redundant ASSERT(obj->IsObject()); these are removed as ARGS_THIS subsumes their functionality.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@monsanto monsanto changed the title buffer: fix toString when this is not a Buffer. buffer: fix segfaults when this is not a Buffer. Apr 21, 2015
bnoordhuis
bnoordhuis reviewed Apr 21, 2015
View reviewed changes
Comment thread src/node_buffer.cc Outdated

@bnoordhuis bnoordhuis Apr 21, 2015

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of control flow in macros and I think that the buffer code in general is overdue for an overhaul.

I want to suggest a more structured approach where the JS prototype methods typecheck the this argument before passing it on to their C++ counterparts. I imagine it would look something like this (using CamelCase for dramatic effect):

const binding = process.binding('buffer');
const BindingIsBuffer = binding.IsBuffer;
const BindingToString = binding.ToString;
const BindingUtf8Slice = binding.Utf8Slice;
// ...

// keep a reference to Object#toString, the user may monkey-patch it
const ObjectProtoToString = Object.prototype.toString;

function IsBuffer(obj) {
  return typeof(obj) === 'object' && obj !== null && BindingIsBuffer(obj);
}

// ...

Buffer.prototype.toString = function() {
  if (!IsBuffer(this))
    return ObjectProtoToString(this);

  if (this.length === 0)
    return '';

  return BindingToString(this);
};

Buffer.prototype.utf8Slice = function(start, end) {
  if (!IsBuffer(this))
    throw new TypeError('this is not an instance of Buffer');

  // validate and coerce start and end

  if (this.length === 0 && start === 0 && end === 0)
    return '';

  return BindingUtf8Slice(this, start, end);
};

// etc.

In short, be paranoid and rigorous. It can be more paranoid still; for example, the user may have replaced this.length with a getter so it would be prudent to read it only once.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@monsanto monsanto Apr 21, 2015

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good approach. setupBufferJS needs to go if we do this. It's weird that part of the methods are added in C++ land and some are added in JS land anyway.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@trevnorris trevnorris Apr 21, 2015

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bnoordhuis' approach is the correct one. when I first rewrote Buffer I did it that way because the diff was already huge and using setupBufferJS was less diff noise in the PR. Plus it maintained how things had been done before.

@bnoordhuis you have plans to rewrite Buffer, or want me to? actually, um. do we just want to hold off on this since Buffer has to be completely rewritten anyway since 4.4 will break everything?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Fishrock123 Fishrock123 Apr 21, 2015

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

um. do we just want to hold off on this since Buffer has to be completely rewritten anyway since 4.4 will break everything?

If it's gona be re-written might as well re-write it to use ArrayBuffers TypedArrays by the sounds of it. :S

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@trevnorris trevnorris Apr 21, 2015

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fishrock123 problem is that's a massive break. Don't even think nan can deal with it in a backwards compatible way. Since class won't be out until 2 (since we're increasing the major, i guess...). Are we going to require this be a 3 release?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Fishrock123 Fishrock123 Apr 21, 2015

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since class won't be out until 2 (since we're increasing the major, i guess...).

That is actually about now. Class-in-strict will be live in 2.0 due to 4.2, iirc.

Are we going to require this be a 3 release?

Well, v8 4.3 is already going to breaking enough to warrant a 3.0, right? :/

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@trevnorris trevnorris Apr 21, 2015

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so stupid. whatever.

on a technical note, we'll need to wait until 2.0 until Buffers are implemented as Typed Arrays. That can be yet another breaking change added to the 3.x list.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@monsanto monsanto Apr 21, 2015

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so what should I do with this PR? It is no problem for me to rewrite this in @bnoordhuis' style, but if that would not be useful due to the V8 4.4 breakage I can also not bother.

If there is going to be another patch release before V8 4.4 & the rewrite I recommend at least taking my first two commits (the .toString() fix and the .copy() fix). They are small, don't touch the ARGS_THIS macro, and fix problems that are easy to trigger. The copy bug is especially bad because anyBuffer.copy(obj.misspelledProperty) segfaults.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mscdex

mscdex commented Apr 21, 2015

Copy link
Copy Markdown
Contributor

I'm not 100% sure about what the current status is on LTS releases, but I would imagine that even though Buffer is going to be rewritten/heavily modified because of upcoming V8 changes, that these changes could at least land in 1.x?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trevnorris

trevnorris commented Apr 21, 2015

Copy link
Copy Markdown
Contributor

@iojs/tc Thoughts on making sure this change gets in to v1.x regardless of whether a v2 is coming out soon?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fishrock123

Fishrock123 commented Apr 21, 2015

Copy link
Copy Markdown
Contributor

@trevnorris land in master and backport?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trevnorris

trevnorris commented Apr 22, 2015

Copy link
Copy Markdown
Contributor

Fine by me. Will need the changes @bnoordhuis explained though.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@monsanto

monsanto commented Apr 24, 2015

Copy link
Copy Markdown
Contributor Author

It looks like the copy() part of this PR was reported, and a fix was landed immediately. I asked here 3 days ago if people would cherry pick this fix, but I was ignored, and instead asked to rewrite a good bit of Buffer. It isn't a big deal to me since it was such a small amount of code, but it still feels a bit rude since I know @trevnorris was aware of this PR, and I now feel like I've been led on a bit of a wild goose chase wrt the rewrite.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trevnorris

trevnorris commented Apr 24, 2015

Copy link
Copy Markdown
Contributor

@monsanto I'd like to apologize for the confusion. This PR popped up the same day I learned V8 was removing the API that Buffer currently uses, and so was heavily on my mind. And my comments about the changes @bnoordhuis suggested were mainly about the technique he was explaining.

The reason the copy() fix landed was because someone pinged me about that specific case, and TBH I forgot about this PR (I forget about my own PRs constantly, so please don't be offended). That individual fix was very straight forward. The change here is a little more in depth.

If there's a quick fix to prevent a user from being able to segfault from JS then it should land. The user should never be able to segfault from JS using blessed API. I'll finish doing my review now.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

trevnorris
trevnorris reviewed Apr 24, 2015
View reviewed changes
Comment thread lib/buffer.js Outdated

@trevnorris trevnorris Apr 24, 2015

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: no need for the { }.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@monsanto

monsanto commented Apr 24, 2015

Copy link
Copy Markdown
Contributor Author

@trevnorris it's fine, and don't worry about reviewing the changes, it's changed up a lot locally due to moving the checks to JS land as requested. I didn't have time to finish up that work yesterday but I can try to get it out today.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

monsanto added 2 commits April 25, 2015 06:01
@monsanto
move all prototype initialization to JS
9f61af1
@monsanto
validate buffer instance consistently
00da460
@monsanto

monsanto commented Apr 25, 2015

Copy link
Copy Markdown
Contributor Author

@trevnorris @bnoordhuis I have moved all type checks (and a few others) to JS land.

I have less free time than I expected so the code is not as paranoid as requested. For example, it uses b instanceof Buffer (the established norm in the file) instead of a more involved C++ land check. There are also some validations in C++ land that could have been moved to JS land. Since the code is slated to be rewritten it is hard to be motivated about covering all of these bases, I am just interested in fixing the segfaults in the most straightforward way possible.

Hopefully this is useful, and the extra paranoia/etc can be added in a follow up PR. Otherwise I am going to have to bow out. Sorry!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fishrock123

Fishrock123 commented Apr 25, 2015

Copy link
Copy Markdown
Contributor

Some CI: https://jenkins-iojs.nodesource.com/view/iojs/job/iojs+any-pr+multi/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

trevnorris
trevnorris reviewed Apr 27, 2015
View reviewed changes
Comment thread lib/buffer.js

@trevnorris trevnorris Apr 27, 2015

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though this case is insane, it inevitably will break someone in the wild. Because of that this is a sem-minor patch.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@monsanto monsanto Apr 27, 2015

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which part will break? if this is not a Buffer the process will crash

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@trevnorris trevnorris Apr 27, 2015

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hehe. okay true enough. in that case I'd consider this a bug fix patch. :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Fishrock123

Fishrock123 commented Apr 27, 2015

Copy link
Copy Markdown
Contributor

Oops, I just realized my link didn't link to the actual CI run. :S

https://jenkins-iojs.nodesource.com/view/iojs/job/iojs+any-pr+multi/590/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

trevnorris
trevnorris reviewed Apr 27, 2015
View reviewed changes
Comment thread src/node_buffer.cc

@trevnorris trevnorris Apr 27, 2015

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't exclude the proper checks from C++. example:

function F() { }
F.prototype.__proto__ = Buffer.prototype;
var f = new F();

f.write();  // ABORT

The above passes the instanceof check. The only think you can ensure is that it's an Object, but not that it's been allocated external memory. Still run the Buffer::HasInstance() check on all incoming objects.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Fishrock123

Fishrock123 commented May 12, 2015

Copy link
Copy Markdown
Contributor

ping @monsanto

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@monsanto

monsanto commented May 20, 2015

Copy link
Copy Markdown
Contributor Author

sorry @Fishrock123, this PR went from a 5 min fix to something much larger and I don't have the time for it anymore esp. considering this will all be moot for the Buffer rewrite. If someone would like to take the code I have here as their own feel free.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bnoordhuis
bnoordhuis force-pushed the master branch from 311c3e4 to b926718 Compare June 2, 2015 17:25
@brendanashworth brendanashworth mentioned this pull request Jun 9, 2015
buffer: prevent failed assertions #1922
Closed
@trevnorris

trevnorris commented Jun 9, 2015

Copy link
Copy Markdown
Contributor

@monsanto Thanks for all your work on this. You plan on picking it back up, or mind if I make a PR implementing some similar changes you have here?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@monsanto

monsanto commented Jun 9, 2015

Copy link
Copy Markdown
Contributor Author

@trevnorris it's all yours!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trevnorris trevnorris mentioned this pull request Jun 18, 2015
buffer: prevent abort on bad proto #2012
Closed
@rvagg
rvagg force-pushed the master branch from 9d21135 to 628a3ab Compare June 25, 2015 09:18
trevnorris added a commit that referenced this pull request Jun 25, 2015
@trevnorris
buffer: prevent abort on bad proto
1cd9eeb
If an object's prototype is munged it's possible to bypass the
instanceof check and cause the application to abort. Instead now use
HasInstance() to verify that the object is a Buffer, and throw if not.

This check will not work for JS only methods. So while the application
won't abort, it also won't throw.

In order to properly throw in all cases with toString() the JS
optimization of checking that length is zero has been removed. In its
place the native methods will now return early if a zero length string
is detected.

Ref: #1486
Ref: #1922
Fixes: #1485
PR-URL: #2012
Reviewed-By: Ben Noordhuis <[email protected]>
@trevnorris

trevnorris commented Jun 25, 2015

Copy link
Copy Markdown
Contributor

Done in 1cd9eeb.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trevnorris trevnorris closed this Jun 25, 2015
mscdex pushed a commit to mscdex/io.js that referenced this pull request Jul 9, 2015
@trevnorris @mscdex
buffer: prevent abort on bad proto
40c334d
If an object's prototype is munged it's possible to bypass the
instanceof check and cause the application to abort. Instead now use
HasInstance() to verify that the object is a Buffer, and throw if not.

This check will not work for JS only methods. So while the application
won't abort, it also won't throw.

In order to properly throw in all cases with toString() the JS
optimization of checking that length is zero has been removed. In its
place the native methods will now return early if a zero length string
is detected.

Ref: nodejs#1486
Ref: nodejs#1922
Fixes: nodejs#1485
PR-URL: nodejs#2012
Reviewed-By: Ben Noordhuis <[email protected]>
@snyk-bot snyk-bot mentioned this pull request Jul 14, 2021
[Snyk] Upgrade protobufjs from 6.8.8 to 6.11.2 Zintexiss/node-22#27
Open
@snyk-bot snyk-bot mentioned this pull request Feb 12, 2022
[Snyk] Upgrade protobufjs from 6.8.8 to 6.11.2 Iceymann18777/node#9
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

buffer Issues and PRs related to the buffer subsystem.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

isNaN(Buffer.prototype) crashes

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@monsanto @mscdex @JacksonTian @trevnorris @Fishrock123 @bnoordhuis
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.