Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Retry as sudo' doesnt work #234311

Closed
paul0ss opened this issue Nov 20, 2024 · 15 comments
Closed

'Retry as sudo' doesnt work #234311

paul0ss opened this issue Nov 20, 2024 · 15 comments
Assignees
Labels
confirmation-pending file-io File I/O info-needed Issue requires more information from poster workbench-run-as-admin Issues concerning running as administrator

Comments

@paul0ss
Copy link

paul0ss commented Nov 20, 2024

Does this issue occur when all extensions are disabled?: Yes/No

  • VS Code Version: 1.95.3 x64
  • OS Version: Kali Linux 6.11.2-amd64
  • VS Code source: Microsoft Repository

Steps to Reproduce:

  1. Open a file that is not writable by the user
  2. Edit the file
  3. Try to save it
  4. Error pops up stating that not enough permissions
  5. Retry as Sudo option appears
  6. Enter password
  7. Saving process freezes up

People discussing this issue here: #70403
Given information from the above-mentioned source I was able to debug the problem.

According to the developer the write a file with sudo goes through following process:

  1. we write the desired contents into a temp file (here)
  2. we import sudo-prompt (here)
  3. we ask it to run "code --file-write tmpfile target"

After running VSCode with --verbose flag I have noticed the target file is not set:
[sudo-prompt] running command: "/usr/share/code/bin/code" --file-write "/home/paul0ss/.config/Code/code-elevated-DYqaVPH4"

This is probably the reason why the file write doesnt succeed but also doesnt throw any exception stating that permission was denied.

@bpasero
Copy link
Member

bpasero commented Nov 21, 2024

We changed the solution to only require 1 file, so the execution looks fine.

Are there more errors in the full logs?

@bpasero bpasero added info-needed Issue requires more information from poster confirmation-pending labels Nov 21, 2024
@bpasero bpasero added workbench-run-as-admin Issues concerning running as administrator file-io File I/O labels Nov 21, 2024
@paul0ss
Copy link
Author

paul0ss commented Nov 21, 2024

We changed the solution to only require 1 file, so the execution looks fine.

Are there more errors in the full logs?

No other errors. Here is bigger a snippet from the log beginning with the initial permission denied error:

[225780:1121/122623.787264:INFO:CONSOLE(35)] "%c  ERR color: #f33 [text file model] handleSaveError(10) - exit - resulted in a save error: Error: Unable to write file '/home/paul0ss/myfile.txt' (NoPermissions (FileSystemError): Error: EACCES: permission denied, open '/home/paul0ss/myfile.txt') file:///home/paul0ss/myfile.txt", source: vscode-file://vscode-app/usr/share/code/resources/app/out/vs/workbench/workbench.desktop.main.js (35)
[225780:1121/122623.789529:INFO:CONSOLE(3086)] "Failed to save 'myfile.txt': Insufficient permissions. Select 'Retry as Sudo' to retry as superuser.", source: vscode-file://vscode-app/usr/share/code/resources/app/out/vs/workbench/workbench.desktop.main.js (3086)
[225780:1121/122623.791898:INFO:CONSOLE(35)] "%cTRACE color: #888 [text file model] save() - exit file:///home/paul0ss/myfile.txt", source: vscode-file://vscode-app/usr/share/code/resources/app/out/vs/workbench/workbench.desktop.main.js (35)
[225780:1121/122625.149757:INFO:CONSOLE(35)] "%cTRACE color: #888 [text file model] save() - enter file:///home/paul0ss/myfile.txt", source: vscode-file://vscode-app/usr/share/code/resources/app/out/vs/workbench/workbench.desktop.main.js (35)
[225780:1121/122625.149971:INFO:CONSOLE(35)] "%cTRACE color: #888 [text file model] doSave(10) - enter with versionId 10 file:///home/paul0ss/myfile.txt", source: vscode-file://vscode-app/usr/share/code/resources/app/out/vs/workbench/workbench.desktop.main.js (35)
[225780:1121/122625.161301:INFO:CONSOLE(35)] "%cTRACE color: #888 [text file model] doSave(10) - before write() file:///home/paul0ss/myfile.txt", source: vscode-file://vscode-app/usr/share/code/resources/app/out/vs/workbench/workbench.desktop.main.js (35)
[main 2024-11-21T11:26:25.175Z] [sudo-prompt] running command: "/usr/share/code/bin/code" --file-write "/home/paul0ss/.config/Code/code-elevated-PacaoCZO"
[225780:1121/122625.264863:INFO:CONSOLE(35)] "%cDEBUG background: #eee; color: #888 Comments: URIs of continue on comments to add to storage .", source: vscode-file://vscode-app/usr/share/code/resources/app/out/vs/workbench/workbench.desktop.main.js (35)
[main 2024-11-21T11:26:29.248Z] menubarService#updateMenubar 1
[225780:1121/122632.503243:INFO:CONSOLE(35)] "%cDEBUG background: #eee; color: #888 Comments: URIs of continue on comments to add to storage .", source: vscode-file://vscode-app/usr/share/code/resources/app/out/vs/workbench/workbench.desktop.main.js (35)
[main 2024-11-21T11:26:33.616Z] update#checkForUpdates, state =  idle
[main 2024-11-21T11:26:33.618Z] update#setState checking for updates
[main 2024-11-21T11:26:33.620Z] resolveShellEnv(): skipped (VSCODE_CLI is set)
[main 2024-11-21T11:26:33.620Z] [network] #2: https://update.code.visualstudio.com/api/update/linux-x64/stable/f1a4fb101478ce6ec82fe9627c43efbf9e98c813 - begin undefined Xy { b: {}, a: [Object: null prototype] {} }
[main 2024-11-21T11:26:33.638Z] [network] #2: https://update.code.visualstudio.com/api/update/linux-x64/stable/f1a4fb101478ce6ec82fe9627c43efbf9e98c813 - end undefined 204 {
  'access-control-allow-origin': '*',
  date: 'Thu, 21 Nov 2024 11:26:30 GMT',
  'request-context': 'appId=cid-v1:8109e553-6232-492a-afca-b78cea9b431c',
  'strict-transport-security': 'max-age=31536000; includeSubDomains',
  'x-azure-ref': '20241121T112630Z-179d85bf68cp9sr2hC1FRAfyv40000000250000000022uwr',
  'x-cache': 'CONFIG_NOCACHE',
  'x-content-type-options': 'nosniff',
  'x-powered-by': 'Express',
  'x-source-commit': 'ec889288e272a7cf2166c86c73aa9424bf003814'
}

VS Codes behavior:
Image

Full log file:
runas.log

PS: The Apparmor is turned off to cut off any third party interference and the bug was replicated on a fresh Kali install on another computer as well to exclude the possibility of the bug occurring in my specific environment.

@bpasero
Copy link
Member

bpasero commented Nov 21, 2024

Thanks, this line is relevant:

�[90m[main 2024-11-21T11:26:25.175Z]�[0m [sudo-prompt] running command: "/usr/share/code/bin/code" --file-write "/home/paul0ss/.config/Code/code-elevated-PacaoCZO"

Curious when you try this again, can you check the contents of this file and if it exists on disk? It would have a different name each time you try, so you need to get it from the logs.

@KazeEnji
Copy link

Putting my hat in the ring too. I'm running into the same issue. If there's anything I can do to help troubleshoot, let me know otherwise I'll just lurk.

Thanks!

@paul0ss
Copy link
Author

paul0ss commented Nov 21, 2024

Thanks, this line is relevant:

�[90m[main 2024-11-21T11:26:25.175Z]�[0m [sudo-prompt] running command: "/usr/share/code/bin/code" --file-write "/home/paul0ss/.config/Code/code-elevated-PacaoCZO"

Curious when you try this again, can you check the contents of this file and if it exists on disk? It would have a different name each time you try, so you need to get it from the logs.

I have tried all of this. It does exist on the file system.
I also tried writing to the target file using the call defined before and sudo: code --file-write tmpfile target

The command looked like this and the write was successful:
sudo code --file-write /home/paul0ss/.config/Code/code-elevated-HKNtGhiB /home/paul0ss/myfile.txt

That why I assumed that the bug was caused by the absence of the target parameter.

The content of the file looks like the following:
{"source":"/home/paul0ss/.config/Code/code-elevated-bbYNX0yS","target":"/home/paul0ss/myfile.txt"}

It points to another file that also exists on the file system that contains the edited file (how myfile.txt was supposed to look like after saving).

The weirdest thing is that even if I execute the command manually without specifying the target file (BUT obviously with sudo) it still works and myfile.txt gets edited as expected:
sudo code --file-write /home/paul0ss/.config/Code/code-elevated-HKNtGhiB

Maybe the EUID is not set properly? I have noticed that each line of log contains the PID at the beginning of the line. I wanted to check the EUID of the process that attempts the write, but... The line that logs the write doesn't log the PID
Image

@icemanphd
Copy link

I have the same issue, I ran the below so you can see my os vers

──(kali㉿kali)-[~/vtest]
└─$ uname -r
6.8.11-amd64

┌──(kali㉿kali)-[~/vtest]
└─$ uname -a
Linux kali 6.8.11-amd64 #1 SMP PREEMPT_DYNAMIC Kali 6.8.11-1kali2 (2024-05-30) x86_64 GNU/Linux

@bpasero
Copy link
Member

bpasero commented Nov 21, 2024

We rely on a node module for the actual execution of the sudo script: https://github.com/bpasero/sudo-prompt which is forked from https://github.com/jorangreef/sudo-prompt because the latter is not active anymore.

Would anyone here be willing to conduct an experiment purely with node.js and this module to see if the issue is actually with said module? I am starting to feel its in there and not in VS Code.

This would require to follow along the sample in https://github.com/jorangreef/sudo-prompt?tab=readme-ov-file#usage, but I can help if someone is willing to try it out 🙏

@KazeEnji
Copy link

I can help out. I'm running a Debian based OS though and it looks like the instructions you linked for sudo-prompt are for the npm package manager? I'll need your help with what you'd like me to do.

@bpasero
Copy link
Member

bpasero commented Nov 22, 2024

Yes, this would involve installing node.js and being able to run a simple sudo.js file that depends on sudo-prompt package to follow along, are you able to set that up?

@KazeEnji
Copy link

So I'm not sure if I did it correctly:

  • I installed node.js with sudo apt install nodjs
  • I installed npm just to try to align with the instructions in the repo with sudo apt install npm
  • I installed sudo-prompt with npm install sudo-prompt
  • I copied the simple usage script from the sudo-prompt readme to sudo.js.
  • I then ran sudo.js with node sudo.js.

I received a popup to put in my password but after entering it, the terminal is now frozen and one of my cores is spiked and has been for the past few minutes. I wanted to give it time just in case but it wasn't doing anything. Based on the command in the readme, it looks like it should have just echoed hello to the screen right? Or am I way off base?

I took a snapshot of my VM before I did all this so if I need different steps, just let me know and I can rollback easily.

@bpasero
Copy link
Member

bpasero commented Nov 22, 2024

@KazeEnji yeah some tweaks needed, but great progress! please use this code:

var sudo = require('@vscode/sudo-prompt');
var options = {
  name: 'Electron'
};
sudo.exec('echo hello', options,
  function(error, stdout, stderr) {
    if (error) throw error;
    console.log('stdout: ' + stdout);
  }
);

and thus use @vscode/sudo-prompt as NPM package. the other one is not the one VS Code is using anymore.

After a login prompt I get to see:

Image

@KazeEnji
Copy link

KazeEnji commented Nov 22, 2024

Ok, I created the index.js file using the code you provided and when I attempted to run it, I received this error:

node:internal/modules/cjs/loader:1228
  throw err;
  ^

Error: Cannot find module '@vscode/sudo-prompt'
Require stack:
- /home/kali/Documents/Ch20/index.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1225:15)
    at Module._load (node:internal/modules/cjs/loader:1051:27)
    at Module.require (node:internal/modules/cjs/loader:1311:19)
    at require (node:internal/modules/helpers:179:18)
    at Object.<anonymous> (/home/kali/Documents/Ch20/index.js:1:12)
    at Module._compile (node:internal/modules/cjs/loader:1469:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1548:10)
    at Module.load (node:internal/modules/cjs/loader:1288:32)
    at Module._load (node:internal/modules/cjs/loader:1104:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/home/kali/Documents/Ch20/index.js' ]
}

Node.js v20.18.0

Looking the error up, it seems I also needed to npm install @vscode/sudo-prompt? (Stealth Edit: I just reread your post and yes, you did tell me I need that npm package, I just glossed over that lol) So I did that and reran the index.js script. I didn't get the error anymore but the original behavior is back. My terminal just hangs, one core is spiked, and nothing happens.

@bpasero
Copy link
Member

bpasero commented Nov 22, 2024

Thanks! This does indeed mean that the library does not work on your Linux distribution, either due to a regression or always. We did not change the library at all in many years though.

Copy link

This issue has been closed automatically because it needs more information and has not had recent activity. See also our issue reporting guidelines.

Happy Coding!

@vs-code-engineering vs-code-engineering bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 30, 2024
@bpasero
Copy link
Member

bpasero commented Jan 10, 2025

A workaround for this issue is now documented in #237427 (comment). It originates from a change of the Linux distro in hard limit on the maximum number of open file descriptors, i.e. ulimit -Hn returning a very large value (like 1073741816).

@vs-code-engineering vs-code-engineering bot locked and limited conversation to collaborators Jan 14, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
confirmation-pending file-io File I/O info-needed Issue requires more information from poster workbench-run-as-admin Issues concerning running as administrator
Projects
None yet
Development

No branches or pull requests

4 participants