312 points · by mmsc · 2y ago · 126 comments
https://www.ambionics.io/blog/iconv-cve-2024-2961-p1
People are so creative, I can't help but feel some hope for our future :)
Or maybe if we keep intent out of it; features were added in a time when we all worried less about security and internet implications. I would like to say ‘in the security dark ages’ but we are probably still in that era. ;)
- What people are saying about the filter protocol, and anything else that interprets the files.
- Why do people insist on making all remote file protocols transparent? Fuck that, a file is a file! If you want to support some remote stuff, do that on the OS, where it's opt-in.
- And PHP keeps insisting it's not a web framework while being a web framework. So it has no access control around local file reading, while clearly designing it to be used in a way where only a few directories should be accessible.
- A buffer overflow in glibc, go figure... But yeah, C is perfect and the only issue is that the devs don't know the language well enough or there aren't enough people reviewing this niche library.
- Didn't everybody decide to ditch the pre-unicode encodings from the standard tools? I remember the announcement (that was supposed to include glibc), it made a lot of people angry. Those have been an endless source of CVEs.
There's definite reward in having a 0-day. Either you can get a bounty, or sell it in the hacker-souk.
That "couple of lines of code and 20 minutes" is sort of in the eye of the beholder. If you are a highly-experienced language developer, the fixes are likely to be a lot more obvious, simpler, more comprehensive, and robust, than if you are a relatively junior IC.
There's a bit of street cred for finding a 0day, a bit of glamour about figuring out the puzzle. There's not much for the person who fixes it. I think as an industry it might be worth trying to fix that somehow.
Hackers write the worst code, but all the mess needs only one successful hit to become a 0day.
For example, look at the glibc/iconv CVE some other user posted[1]. In the section "Out-of-bound write when converting to ISO-2022-CN-EXT" they have mapped out the boundary checks. By diagnosing the problem this detailed, they already did 90% of the work. The other 10% are the patch and writing to the mailing list.
Surely someone doing all this would already have submitted a patch if they felt comfortable.
It like no one cares to keep a kitchen clean, or a factory in order, until the inspection shows up and closes doors.
Naturally even for those, we are at various levels of how those inspections are honestly enforced across the globe.
But to get your fix in, you'd have to interact with the PHP ecosystem.
As of now, PHP emits an error if arguments are missing but not if there are too many.
A way to bake that in without breaking old code would be to allow function definition to put an explicit stop to the argument list, for example using the void type keyword:
function foo (int $a, string $b, void) : bool
{ ... }
A few month ago I discussed this on the development mailing list and people seemed to agree and even suggested that this would be a good idea by default without the keyword thing I suggested. But I never got the time to properly write an RFC. There is already an old one from years ago that was voted against but In was told it was from before anything strict and typing related was considered important in PHP. If anyone's up to it, please write this RFC :) !<?php declare(strict_args=1);
https://www.php.net/manual/en/class.allowdynamicproperties.p...
You can, within a library, provide an additional argument to a callback without actually introducing a BC break for all users.
My favorite approach would be allowing too many args on dynamic calls (closures, and function calls with dynamic name, not method calls in general) and otherwise rejecting it.
"function current_thing(id: Int, callback: Fn(Int)) {}" and, when you decide you need more you have a myriad of options to add these. From the despised "function current_real_thing(id: Ind, success_callback: Fn(Int), error_callback: Fn(Error)) {}" to "some_namespace:current_thing(...)" via "OtherClass::current_thing(...)" to "load current_thing from NewImplementationOfThing" and so on.
Being strict and explicit isn't opposed to being flexible. And strictness and explicitness is most often a predicament to allow for future change, rather than hampering that future change.
It's far easier to refactor, maintain, test and reason about strict and limited implementations than to do so with dynamic, runtime-magically-changing implementations.
But I have no hard evidence at hand, only how I experienced that in PHP.
Statistics based on packagist(composer)
https://stitcher.io/blog/php-version-stats-january-2024
Statistics based on web servers in the wild
`if ($a) { $previouslyUndefined = 2; } if ($previouslyUndefined) { echo "yeah"; }`
PHP still knows what $previouslyUndefined is or isn't at the second if statement, but it'll throw an error now in the first statement if you hadn't declared it outside the block. Why? Who cares? Scope in PHP is still understood to be inline, not in block; there is no equivalent to let vs var in JS. Stop telling me where I can check a variable if you're not enforcing various kinds of scope.
> A way to bake that in without breaking old code would be to allow function definition to put an explicit stop to the argument list, ...
It's also always preferred to not add anything to the language imo; in this case, I'd opt to have the interpreter emit a warning or info message. It's not broken, it's a developer error.
Oh, and doing this would literally break class autoloading in symfony, and even the engine itself, which relies on this feature.
Not for builtin PHP functions which already throw errors on arity mismatch.
> this would literally break class autoloading in symfony, and even the engine itself, which relies on this feature
I don't understand. Could you point to where in the Symfony code it relies on being able to wrongly call a function with more arguments than it expects and will use?
For variadic functions there is the ... operator already in the language since version 5.6, and my proposal wouldn't break that. Also note that builtin functions already emit deprecated warning in PHP 8 when called with too many arguments.
Here is the full thread discussing it on the PHP internal mailing list: https://news-web.php.net/php.internals/122928
It still only happens during run time. Having a never-called function with an incorrect number of arguments is not an error.
Regardless of the ‘improve the language angle’: Is somebody isn’t running PHPStan (or Psalm, Sonar, etc), then they’re missing out.
PHPStan is currently so good that using it should be non-negiotable. So the question would then even be: “I’d like rule 123 of the tool to be native, we helps with the RFC?”
$a = &$arr[]
Gives you a reference to null and appends it to the array. Then you can pass $a to something to mutate the tail “from a distance”. Most people never need this, nor should they use it. But when you are writing a streaming parser, it is quite handy as a one-liner instead of writing $a = null
$arr[] = &$a
or keeping track of the current index and dealing with off-by-one errors.For applications, these static analysis tools are great. For libraries, not so much.
Big Oof. :( :( :(
why would i accept performance penalty if i don't allow open('https://google.com') to begin with?
the correct action would be to remove all the stupid features everyone serious disable to begin with.
PHP is still lacking, there is no unicode support, and for a web language this is really bad. Also, the way PHP functions, makes modern web (like websockets) use impossible, there is hacks around this but they all kind of suck.
The important - CPU intensive parts - of numpy, pandas, pytorch, and all the other "fast python" libraries out there, are actually written in C.
Pure python should not be used for anything that requires good performance: it is programmer ergonomic, not CPU ergonomic. It is great that through the use of FFIs it has access to powerful libraries written in a language that isn't slow, but that does not make it as a language itself, fast.
For an interpreted language PHP itself is ridiculously fast and the VM is rather small so you can use something else coughElixircough for parallellisation. I use it all the time for data wrangling stuff and database imports because it's robust, fast and PsySh is a pretty neat environment.
The array data structure is quite nice too. It's built on simple parts that are foundational to the VM itself, and very flexible, similar to lists in Lisp-like languages but without the seek lag when data grows due to the indexing.
I’ll have to check this out.
Though not popular php can be a surprising decent scripting langauge.
That is not why.
You stick to python because it's your common denominator. You all picked it up in school.
Python the most popular language around, and one of the slowest.
Python only seems fast because all the heavy duty number crunching libraries are actually written in C.
By far, for most apps, the biggest bottleneck is the database.
In my experience, it’s actually very fast. That may be partly because of the way I write the code, though, and my backend code isn’t really too massive.
Like it or not, exploiting seems just more fun and rewarding. A lot of people will be interested to learn on your blog how you came to find and exploit a vulnerability. The 10 line of code patch gets little attention. Not even taking into consideration bug bounties...
sudo apt-get purge php.*
``` project/ ├── data/ │ └── surveys.json ├── app.js └── package.json ```
### Шаг 1: Инициализация проекта
1. Создаем папку проекта и переходим в неё:
```bash
mkdir project
cd project
```
2. Инициализируем npm: ```bash
npm init -y
```
3. Устанавливаем Express.js: ```bash
npm install express
```
### Шаг 2: Создаем файл `surveys.json` для хранения опросовСоздаем папку `data` и файл `surveys.json` в ней с начальным содержимым:
```json [] ```
Это будет массив объектов, где каждый объект — отдельный опрос.
### Шаг 3: Создаем файл `app.js` для серверной логики
Вот код для `app.js`:
```javascript const express = require('express'); const fs = require('fs'); const app = express();
app.use(express.json());
const surveysFilePath = './data/surveys.json';
// Функция для чтения данных из файла const readSurveys = () => { const data = fs.readFileSync(surveysFilePath, 'utf-8'); return JSON.parse(data); };
// Функция для записи данных в файл const writeSurveys = (surveys) => { fs.writeFileSync(surveysFilePath, JSON.stringify(surveys, null, 2)); };
// Создание нового опроса app.post('/surveys', (req, res) => { const surveys = readSurveys(); const newSurvey = { id: Date.now(), ...req.body, editable: true }; surveys.push(newSurvey); writeSurveys(surveys); res.status(201).json(newSurvey); });
// Получение всех опросов app.get('/surveys', (req, res) => { const surveys = readSurveys(); res.json(surveys); });
// Редактирование опроса (если он редактируемый) app.put('/surveys/:id', (req, res) => { const surveys = readSurveys(); const surveyId = parseInt(req.params.id); const surveyIndex = surveys.findIndex(survey => survey.id === surveyId);
if (surveyIndex === -1) {
return res.status(404).json({ error: 'Survey not found' });
}
if (!surveys[surveyIndex].editable) {
return res.status(403).json({ error: 'Survey cannot be edited' });
}
surveys[surveyIndex] = { ...surveys[surveyIndex], ...req.body, editable: false };
writeSurveys(surveys);
res.json(surveys[surveyIndex]);
});// Удаление опроса app.delete('/surveys/:id', (req, res) => { const surveys = readSurveys(); const surveyId = parseInt(req.params.id); const newSurveys = surveys.filter(survey => survey.id !== surveyId);
if (newSurveys.length === surveys.length) {
return res.status(404).json({ error: 'Survey not found' });
}
writeSurveys(newSurveys);
res.status(204).send();
});// Запуск сервера const PORT = 3000; app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); }); ```
### Пояснение к коду:
1. *Маршруты*: - `POST /surveys` – создание нового опроса с полем `editable: true`. - `GET /surveys` – получение всех опросов. - `PUT /surveys/:id` – редактирование опроса по ID, если `editable: true`, после чего `editable` становится `false`. - `DELETE /surveys/:id` – удаление опроса по ID.
2. *Функции*: - `readSurveys` – читает данные из JSON-файла. - `writeSurveys` – записывает данные в JSON-файл.
### Шаг 4: Запуск проекта
Запустите сервер командой:
```bash node app.js ```
Теперь API будет доступен по адресу `http://localhost:3000`.
The last part makes the fact their could be massive security holes like RCE in the core language very worrying.
Going from php 4 to php 8 isn't even that painful and there are twenty years between. It's the one language where what you wrote 20 years ago probably still works today.
Upgrading a php application is one of the least expensive and uneventful things you can do. Try upgrading that java swing application or that react app that's 15 months old. Go, haskell, vue, python 2 to 3 are more difficult because of syntax changes where in php you have some breaking changes like globals being removed or ereg removal or mysql_ being removed. The changes were small like using mysqli instead of mysql or using preg instead of ereg.
Now... that's also a problem in other languages too, but I've found it less so with something like... older Java. Because there are some things that are completely gone from earlier PHP, if people relied on those bits 15 years ago, there's potentially a lot of code touching that needs to happen.
My experience is there's less deprecation of language features in older languages (mostly thinking of Java). There were perhaps less 'wonky' ways of doing dynamic Java stuff in 2006, so 2006 Java will still work today. 2006 PHP can work today (see comments below), but the more 'advanced' your PHP was the more at-risk it will be when trying to upgrade to, say, PHP 8.x.
The other big thing, though, is frameworks. The more you tied your code to, say, ZF1, the bigger an upgrade effort will be (sometimes far bigger than expected). I've been hit by this a couple times in the last few years.
There are literally companies stuck on PHP 7 because going to 8 is too painful. And honestly with my decade of experience your claim that going from 4 to 8 isn’t that painful sounds like nonsense and something you haven’t done. And the claim that php 4 code will work on php 8 is 100% nonsense. The syntax for how classes were defined in 4 is not supported in 8.
I’ve upgraded the code to the major versions of other languages without hassle. But they generally had fewer breaking changes in their major versions when I upgraded to Vue3 which was super painless.
And can you point to a minor upgrade where Go changed the syntax to the point the old one no longer works? With Go normally it's the tooling that changes. I can't remember the syntax changing on me. Especially since they have a Compatability Promise[1] it seems weird.
I know that they were still using the old code, just a year or so ago, on PHP 8.
Should be noted that it stopped being the case close to a decade ago now. Since PHP 8 things have changed a lot and it's a significantly better platform, both in terms of usage and the people behind it.
PHP spent a long time running on fumes with little backing. It's now got huge financial backing from Jetbrains, Wordpress Symfony, Laravel etc and theres now people paid to work on it, which has dramaticly improved the quality and quantity of improvements, which are mostly focused around performance and bug fixes.
The performance gains arent just figures on paper either. There was a real world improvement of around 12% on PHP 8.3 alone.
The breaking changes section of UPGRADING file for PHP 8.4 is over 200 lines. For 8.3 it was over 100 lines.
And when it went to version 8, there was only 1 full-time developer working on it and as far as I know 0 part-time. The rest were volunteers doing it as a hobby. That full-time developer who was paid by Jetbrains decided he wanted to work on another project. This resulted in the next release being pretty much nothing. At which point, everyone realised this language went from funded to not funded and they created the PHP foundation.
Typically it is features before that causes problems.