This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Kizz's profile picture

Override Read Only ROM

Started by Kizz, 21 April 2015 - 02:47 PM
Kizz #1
Posted 21 April 2015 - 04:47 PM
Greetings CCer's

I am currently working on a secure OS called KOS and am trying to override the ability to call /rom/programs/edit

I use this installer: http://pastebin.com/UzfMm1hx

At line 52 you can see where I originally tried moving/deleting the two files which cause a great security risk. However, access is denied. Duh, ROM is read only!

Is there anyway around this issue or a better alternative?

I know you lvl99 Lua wizards can do this… impress me ;D

Thanks!
valithor #2
Posted 21 April 2015 - 05:01 PM
The easiest way I can think of doing this is overwriting fs.open, so that file can not be opened.


local _fs.open = fs.open
local combine = fs.combine
local lower = string.lower

fs.open = function(path,...)
  if lower(combine("/",path)) == "/rom/programs/edit" then -- this might actually just be rom/programs/edit, depends on what fs.combine returns
    return nil
  else
    return _fs.open(path,...)
  end
end

edit:

And as the person below mentioned (which I forgot to say myself)

You can not remove/modify any file in the rom.
Edited on 21 April 2015 - 03:03 PM
SeaLife #3
Posted 21 April 2015 - 05:01 PM
first of all, you CANT delete files out of the /rom/ directory… (With fs.delete(path) or rm <path>)

If someone else know how to do this anyway, i'll delete this shit :D/>

The easiest way I can think of doing this is overwriting fs.open, so that file can not be opened.


local _fs.open = fs.open
local combine = fs.combine
local lower = string.lower

fs.open = function(path,...)
  if lower(combine("/",path)) == "/rom/programs/edit" then -- this might actually just be rom/programs/edit, depends on what fs.combine returns
	return nil
  else
	return _fs.open(path,...)
  end
end

This could work.. (But it wont "delete" the file rom/programs/edit, you just cant "access" them, cause fs.open think, the file does not exists)

Greets
SeaLife
Edited on 21 April 2015 - 03:03 PM
Lupus590 #4
Posted 21 April 2015 - 05:05 PM
there is a utility on the forums which lets you "edit" rom (it copies and "hides" the actual rom)
valithor #5
Posted 21 April 2015 - 05:08 PM
-snip

The question was how to prevent people from accessing the program. How to delete it was just a side statement.

there is a utility on the forums which lets you "edit" rom (it copies and "hides" the actual rom)

This is more of a sandboxing thing, which would just make the os less efficient as it has to run additional code.


edit:
Any solution you find will only be temporary for the current session. That is to say the second you restart the computer the changes you made will be gone.

Although this really does not help you at all if you want to distribute the OS. You can use resourcepacks to modify the contents of rom, but as I said this does not help if you are not going to be the only one using it.
Edited on 21 April 2015 - 03:10 PM
Kizz #6
Posted 21 April 2015 - 05:15 PM
Yea, to be fair, since the user has to log in to do any of this, it is still not a huge issue. I just wanted to know if the wizards of Lua knew something magical. I will poke through some of these experiments and see what I can do.

Again, thank you all!
Kizz #7
Posted 21 April 2015 - 05:45 PM
The easiest way I can think of doing this is overwriting fs.open, so that file can not be opened.


local _fs.open = fs.open
local combine = fs.combine
local lower = string.lower

fs.open = function(path,...)
  if lower(combine("/",path)) == "/rom/programs/edit" then -- this might actually just be rom/programs/edit, depends on what fs.combine returns
	return nil
  else
	return _fs.open(path,...)
  end
end

edit:

And as the person below mentioned (which I forgot to say myself)

You can not remove/modify any file in the rom.

I tried implementing this into my primary runtime and it throws a few errors.

'local _fs.open = fs.open' throws an unexpected symbol.

If I comment out that line, it will work, but will throw an error about failing to yield on the following line:

'if lower(combine("/",path)) == "rom/programs/edit" then'

Once I try and edit the file blocked, it will just crash the shell.

I am assuming you meant for me to actually edit the fs api remotely and implement the fix. But as this is intended to be a lightweight OS, I would rather not have to have people changing the api's. Too complicated and too much effort.
flaghacker #8
Posted 21 April 2015 - 05:53 PM
Your first problem is the fact that you're trying to use a variable name containing a dot.

The second one is really weird, could you post the full code?

The code would look something like this:

local oldOpen = fs.open

fs.open = function (path, mode)
  if isBanned (path)
    return null
  else
    return oldOpen (path, mode)
  end
end
With isBanned being a function that checks is that file/folder is banned (aka doesn't "exist").
valithor #9
Posted 21 April 2015 - 06:03 PM
Yea sorry I actually took that from something where I modify the entire fs API, and forgot to make that small modification. I would still suggest doing it how I did it as if you don't do it that way it can be bypassed.
Kizz #10
Posted 21 April 2015 - 06:30 PM
Boom! The wizards have been a great influence!

By adding this to my runtime:


function protect()
local oldFs = fs.open
  print'OS Protect active!'
  fs.open = function(path,...)
    if path == "rom/programs/edit" or path == "/rom/programs/edit" then
    print'Failed to call edit!'
    return
  end
    if path == "rom/programs/delete" or path == "/rom/programs/delete" then
    print'Failed to call delete!'
    return
  end
  if path ~= "rom/programs/delete" or path ~= "/rom/programs/delete" or path ~= "rom/programs/edit" or path ~= "/rom/programs/edit" then
	    return oldFs(path,...)
  end
end
end

I have managed to eliminate the ability to call those files remotely. Yay!

Thanks for all the help fellas!
Lupus590 #11
Posted 21 April 2015 - 07:10 PM
I'm just going to clean up your code a bit (hope you don't mind)


--#I think i have miss interprited what your bit of code is doing, is it going to overwrite fs.open or is it a function in your os?

local oldFs = fs.open --#having this in your overwriting function will slowly break your program...
--#each time this function is called (overwriting your overwrite with itself, where it needs access to the original)

function protect(path,mode) --to overwrite you need to take the same paramitors
  if path == nil then error("nil path", 2) end --# the 2 tells error to blame the line that called this function

  --#print'OS Protect active!' --#code should not write to the screen unless it manages the screen, leave this to the program
  --#fs.open = function(path,...) --#this line is not needed
  if path == "rom/programs/edit" or path == "/rom/programs/edit" then
	--#print'Failed to call edit!'
	--#error("This file is protected",2) --#erroring is not an nice why of doing this, i'm out of ideas
	return

  elseif path == "rom/programs/delete" or path == "/rom/programs/delete" then
	--#print'Failed to call delete!'
	return
  else --#this replaces the below if
  --#if path ~= "rom/programs/delete" or path ~= "/rom/programs/delete" or path ~= "rom/programs/edit" or path ~= "/rom/programs/edit" then
  --#above if is not needed as you return in the other ones
	   return oldFS(path,mode)
  end
end

fs.open = protect --#you need to overwrite the function that everyone uses so that they now use your modified version
--#end --#this was a spare end
Edited on 21 April 2015 - 06:00 PM
Kizz #12
Posted 21 April 2015 - 07:17 PM
I'm just going to clean up your code a bit (hope you don't mind)


--#edits on the way
function protect()
local oldFs = fs.open
  print'OS Protect active!'
  fs.open = function(path,...)
	if path == "rom/programs/edit" or path == "/rom/programs/edit" then
	print'Failed to call edit!'
	return
  end
	if path == "rom/programs/delete" or path == "/rom/programs/delete" then
	print'Failed to call delete!'
	return
  end
  if path ~= "rom/programs/delete" or path ~= "/rom/programs/delete" or path ~= "rom/programs/edit" or path ~= "/rom/programs/edit" then
			return oldFs(path,...)
  end
end
end

Lol you pasted the same thing?
Ahh I see. Edits on the way. I removed the combine and lowecase functions because they were not needed. I also seperated the various commands due to a crash I suspect was related to the combine function. I prefer the code to be extracted as is so that it is easier to read and follow. Less isn't always more.
Edited on 21 April 2015 - 05:18 PM
valithor #13
Posted 21 April 2015 - 07:17 PM
I'm just going to clean up your code a bit (hope you don't mind)


--#edits on the way
function protect()
local oldFs = fs.open
  print'OS Protect active!'
  fs.open = function(path,...)
    if path == "rom/programs/edit" or path == "/rom/programs/edit" then
    print'Failed to call edit!'
    return
  end
    if path == "rom/programs/delete" or path == "/rom/programs/delete" then
    print'Failed to call delete!'
    return
  end
  if path ~= "rom/programs/delete" or path ~= "/rom/programs/delete" or path ~= "rom/programs/edit" or path ~= "/rom/programs/edit" then
		    return oldFs(path,...)
  end
end
end
I'm just going to clean up your code a bit (hope you don't mind)


--#edits on the way
function protect()
local oldFs = fs.open
  print'OS Protect active!'
  fs.open = function(path,...)
    if path == "rom/programs/edit" or path == "/rom/programs/edit" then
    print'Failed to call edit!'
    return
  end
    if path == "rom/programs/delete" or path == "/rom/programs/delete" then
    print'Failed to call delete!'
    return
  end
  if path ~= "rom/programs/delete" or path ~= "/rom/programs/delete" or path ~= "rom/programs/edit" or path ~= "/rom/programs/edit" then
		    return oldFs(path,...)
  end
end
end
I would like to point out that without fs.combine it is extremely easy to bypass this protection.
For example:

fs.open("//rom/programs/edit","r") -- this will work
-- And by extension 
//rom/programs/edit -- instead of just /rom/programs/edit

Just know you can add the extra /'s anywhere, and in a infinite amount, so it is inefficient to manually block all of them.

edit:

correction, Anywhere that there is already a /
Edited on 21 April 2015 - 05:21 PM
Kizz #14
Posted 21 April 2015 - 07:20 PM
Ahh I see. I can try to re-implement the combine later. It just seemed to cause the shell to crash on load for some reason.
Kizz #15
Posted 21 April 2015 - 07:43 PM
Latest code as per suggestions:


function protect()
local combine = fs.combine
local oldFs = fs.open
--print'OS Protect active!'
fs.open = function(path,...)
  if combine("/",path) == "rom/programs/edit" then
   --print'Failed to call edit!'
   return
  end
  if combine("/",path) == "rom/programs/delete" then
   --print'Failed to call delete!'
   return
  end
  if combine("/",path) ~= "rom/programs/delete" then
   return oldFs(path,...)
  end
end
end

Works fine. Thanks guys!

I'm just going to clean up your code a bit (hope you don't mind)


--#still editing
local oldFs = fs.open --#having this in your overwriting function will slowly break your program each time this function is called (overwriting your overwrite with itself, where it needs access to the original)
function protect()

  --#print'OS Protect active!' --#code should not write to the screen unless it manages the screen, leave this to the program
  fs.open = function(path,...)
  if path == "rom/programs/edit" or path == "/rom/programs/edit" then
	--#print'Failed to call edit!'
	return
  end
  if path == "rom/programs/delete" or path == "/rom/programs/delete" then
	--#print'Failed to call delete!'
	return
  end
  if path ~= "rom/programs/delete" or path ~= "/rom/programs/delete" or path ~= "rom/programs/edit" or path ~= "/rom/programs/edit" then
			return oldFs(path,...)
  end
end

fs.open = protect --#you need to overwrite the function that everyone will use so that they now use your modified version
--#end --#this was a spare end

Adding fs.open = protect causes an error: attempt to index a nil value.
Lupus590 #16
Posted 21 April 2015 - 11:50 PM
I didn't see the function overwrite inside of the protect function (why do you need to do that?)

correction, apparently I did see it, no idea what the error is (I updated it since you quoted)
Edited on 21 April 2015 - 09:52 PM
Kizz #17
Posted 22 April 2015 - 01:49 AM
All that bit of code does is say if you try to run delete or edit remotely by typing /rom/programs/edit or //////////////rom/programs/edit, that it will not allow you.

Just a way to stop people editing OS files.
flaghacker #18
Posted 22 April 2015 - 06:13 AM
All that bit of code does is say if you try to run delete or edit remotely by typing /rom/programs/edit or //////////////rom/programs/edit, that it will not allow you.

Just a way to stop people editing OS files.

No, thats not wat it does, since you can't edit them anyway…

It hides these files from any program that is ran, including the shell. Printibg stuff in that function probably isn't a good idea, because that will mess up gui's. Shell will print an error "file not found" by default.
Edited on 22 April 2015 - 04:16 AM
MKlegoman357 #19
Posted 22 April 2015 - 12:12 PM
If people will be able to run their own programs then what's the point in disabling 'edit' and 'delete'? I could just download a file manager and do whatever I want.

BTW, ROM stands for Read Only Memory, so your title looks interesting :lol:/>
Kizz #20
Posted 22 April 2015 - 02:21 PM
I think you guys are missing that I am simply defining my own edit and delete programs. Not in ROM, but in my own OS's filesystem. This custom edit and delete prevents users editing important OS files or viewing the users files.

The solution I have chosen works perfectly because you now cannot run, edit or use the old methods of editing/deleting.

Since the user has to use my edit and delete, I can rebuild edit and delete to not allow editing or deletion of my own OS files.

As of this moment, I have found my solution.
valithor #21
Posted 22 April 2015 - 04:10 PM
I think you guys are missing that I am simply defining my own edit and delete programs. Not in ROM, but in my own OS's filesystem. This custom edit and delete prevents users editing important OS files or viewing the users files.

The solution I have chosen works perfectly because you now cannot run, edit or use the old methods of editing/deleting.

Since the user has to use my edit and delete, I can rebuild edit and delete to not allow editing or deletion of my own OS files.

As of this moment, I have found my solution.

Might be better to do something similar for your os files, where they can be read but not written to. It would be easy to write up a short edit program using your edit program, to edit os files, or even doing it manually with the fs api.

it would be something like this:

local blockedPaths = { --1 is read only, 2 is no access
  ["/rom/programs/edit"] = 2,
  ["/myOS/file"] = 1,
  ["/myOs/otherFile"] = 1
}
local open = fs.open
local combine = fs.combine

fs.open = function(path,mode)
  for k,v in pairs(blockedPaths) do
    if combine("/",path) == k then
      if v == 1 then
        return open(path,"r") -- defaulting to read mode
      elseif v==2 then
        return nil
      end
    end
  end
  return open(path,mode) -- the path is not blocked
end

This would just be a matter of further preventing the editing of your files. Just know by limiting to read only the file can still be run it just can not be written to. So this would not work for the blocking of the edit program.

With the current restriction someone could use pastebin to get the old edit program (assuming it is enabled). They could also use one of the many auto typers on the forums to enter the program for them. Either way blocking access only in the edit program is bypassable.

edit:

Also something I did not notice at first, simply blocking the delete program in the rom does not block the deleting of files. You would want to overwrite fs.delete similar to how you overwrite fs.open. As long as the user has access to create and run programs, they will have access to these functions. Which means only blocking the program and not the api leaves security holes.
Edited on 22 April 2015 - 02:24 PM