News for hackers. Stuff that matters.
Re:Memory Safe Languages in Android 13 by nneonneo on Thursday December 01, 2022 @03:19PM (#33822128)
Unclear what you mean by user input - for arguments, `std::env::args()` exists, and for stdin `std::io::stdin()` exists and provides various read functions.

    let mut line = String::new();
    stdin().read_line(&mut line)?;
    println!("input: {}", line);
I suspect that there _have_ been some changes since you last looked - for example, the ? operator lets you propagate errors in a more compact way.
Re:Memory Safe Languages in Android 13 by netr0ute on Thursday December 01, 2022 @04:04PM (#33822711)

    let mut line = String::new();
    stdin().read_line(&mut line)?;
This is what I'm talking about. This is so ugly and clunky and needlessly verbose like Java compared to C++ where you can do

    std::string input;
    std::cin >> input;
and have it Just Work. Why can't Rust do something similar?
Re:Memory Safe Languages in Android 13 by nneonneo on Thursday December 01, 2022 @04:20PM (#33822940)
Except, you've failed to check for an error, and you need another line to do that. Suppose the user ended their input or the input is a pipe that's been closed - what does your C++ program do?

Here's another example. C++ lets you do this:

    long input;
    std::cin >> input;
    process(input);
and this is very convenient! It's much shorter than the Rust code for doing the same. It's also wrong! If the input cannot be read, or it cannot be parsed as an integer, `input` now contains undefined content. You'd need to remember to check the contents, or end up processing undefined memory values. You cannot make this error in Rust without a lot of contortions (e.g. unsafe).

Some people will say, I just want to get stuff done and not worry about all this safety junk. As someone who works in security and have exploited bugs in C/C++ programs, this is pretty much why we wound up with so many CVEs.

Re:Memory Safe Languages in Android 13 by netr0ute on Thursday December 01, 2022 @06:02PM (#33824252)
> Some people will say, I just want to get stuff done and not worry about all this safety junk. As someone who works in security and have exploited bugs in C/C++ programs, this is pretty much why we wound up with so many CVEs.

Are constructions like these really the root cause of CVEs? As long as you do a correct check, then you're all good. If Rust can do the same thing in fewer lines, then that may or may not be a good thing because some people might want a lower degree of abstraction or a greater one.

Re:Memory Safe Languages in Android 13 by nneonneo on Thursday December 01, 2022 @08:33PM (#33825612)
Oh absolutely! Here’s an example from 2019: https://www.cve.org/CVERecord?id=CVE-2019-15900

“An issue was discovered in slicer69 doas before 6.2 on certain platforms other than OpenBSD. On platforms without strtonum(3), sscanf was used without checking for error cases. Instead, the uninitialized variable errstr was checked and in some cases returned success even if sscanf failed. The result was that, instead of reporting that the supplied username or group name did not exist, it would execute the command as root.”

Fail to check that your input parsed correctly, wind up with privilege escalation to root.

Re:Memory Safe Languages in Android 13 by netr0ute on Thursday December 01, 2022 @11:07PM (#33826651)
The problem here is that that implementation was written in plain old C and is super unsafe, and is NOT C++. I don't know why C keeps getting confused with C++ because the two are basically totally different now.
Re:Memory Safe Languages in Android 13 by nneonneo on Friday December 02, 2022 @01:17AM (#33827431)
Oh my bad! That must be why C++, a definitely not super unsafe language that is basically totally different from C, lets you do this:

    char buf[10];
    std::cin >> buf;
    std::cout << buf << std::endl;
g++ -Wall -std=c++2a (sorry, I’ve only got GCC 9), no warnings or errors, happily overruns the stack when fed more than 10 characters. This code is essentially equivalent to the C gets(buf) (splitting on whitespace instead of newline), but gets is deprecated and generates big warnings on most modern C compilers. Yet, operator>>(char *) does not.

Sure, you can write secure code in C++. You can write secure code in C too, even if that language is “super unsafe”. But neither language will stop you from writing insecure code either.

Re:Memory Safe Languages in Android 13 by dagmx on Thursday December 01, 2022 @11:23PM (#33826751)
I have to say I appreciate when folks like yourself explain the subtle flaws in very simple code.

It’s been really eye opening as I do security analysis of C++ projects how many foot guns are in a ton of codebases that now need so much scaffolding to handle properly, after they’ve done damage.

With Rust, my first pass is usually the right pass.

Re:Memory Safe Languages in Android 13 by masklinn on Thursday December 01, 2022 @04:25PM (#33823011)
Because Rust does not want to have implicit constructors, default constructors, constructors at all, random statics, ...

And `std::cin` does not read a line from the input, you need `getline` for that.

Re:Memory Safe Languages in Android 13 by oll3 on Thursday December 01, 2022 @05:05PM (#33823600)
Not that this kind of stdin reading operation is something I do often. But at least the Rust code says what it does. Bitshifting stdin by a string on the other hand... Does it read a line, or forever, or read at all? I wouldn't call it the ultimate syntax for a line reader anyway. :)