Re: rm -P and no-write on files - perm denied, bail out?

2022-10-14 Thread Theo de Raadt
Mikolaj Kucharski  wrote:

> Hi,
> 
> Kind reminder. Diff re-attached at the end and on MARC:
> 
> https://marc.info/?l=openbsd-tech=166219807307308=2

I don't understand what your complaint is, because:

 -P  Attempt to overwrite regular writable files before deleting them.
 ^

Well, the attempt fails.

Then rm continues to do what rm does, which specifically is to not leave
files not removed.


> On Sat, Sep 03, 2022 at 09:44:46AM +, Mikolaj Kucharski wrote:
> > Hi,
> > 
> > I wanted to rm -rP some files on my disk and didn't notice that
> > they lacked write permission for the user who executed rm(1)
> > command.
> > 
> > $ echo foo > file-mode-444.txt
> > $ chmod 0444 file-mode-444.txt
> > $ ls -ln file-mode-444.txt
> > -r--r--r--  1 5001  5001  4 Sep  3 09:36 file-mode-444.txt
> > 
> > $ rm -vfP file-mode-444.txt
> > rm: file-mode-444.txt: Permission denied
> > file-mode-444.txt
> > $ echo $?
> > 1
> > 
> > $ ls -l file-mode-444.txt
> > ls: file-mode-444.txt: No such file or directory
> > 
> > I was not expecting this behaviour. My expectation was the file would
> > NOT be removed. Hence the diff below:
> > 
> > 
> > Index: rm.c
> > ===
> > RCS file: /cvs/src/bin/rm/rm.c,v
> > retrieving revision 1.44
> > diff -u -p -u -r1.44 rm.c
> > --- rm.c16 Aug 2022 13:52:41 -  1.44
> > +++ rm.c3 Sep 2022 09:37:44 -
> > @@ -215,8 +215,11 @@ rm_tree(char **argv)
> > case FTS_F:
> > case FTS_NSOK:
> > if (Pflag)
> > -   rm_overwrite(p->fts_accpath, p->fts_info ==
> > -   FTS_NSOK ? NULL : p->fts_statp);
> > +   if (!rm_overwrite(p->fts_accpath, p->fts_info ==
> > +   FTS_NSOK ? NULL : p->fts_statp)) {
> > +   eval = 1;
> > +   continue;
> > +   }
> > /* FALLTHROUGH */
> > default:
> > if (!unlink(p->fts_accpath)) {
> > @@ -267,7 +270,10 @@ rm_file(char **argv)
> > rval = rmdir(f);
> > else {
> > if (Pflag)
> > -   rm_overwrite(f, );
> > +   if (!rm_overwrite(f, )) {
> > +   eval = 1;
> > +   continue;
> > +   }
> > rval = unlink(f);
> > }
> > if (rval && (!fflag || errno != ENOENT)) {
> > 
> > 
> > What do you guys think?
> > 
> > 
> > $ ./obj/rm -vfP file-mode-444.txt
> > rm: file-mode-444.txt: Permission denied
> > $ echo $?
> > 1
> > 
> > $ ls -ln file-mode-444.txt
> > -r--r--r--  1 5001  5001  4 Sep  3 09:36 file-mode-444.txt
> > 
> > I did use `rm -fP` in the invocation, and reading the rm(1) manual page:
> > 
> >-f  Attempt to remove the files without prompting for confirmation,
> >regardless of the file's permissions.  If the file does not
> >exist, do not display a diagnostic message or modify the exit
> >status to reflect an error.  The -f option overrides any previous
> >-i options.
> > 
> > but not sure then what exactly should happen when -P and -f and no write
> > permission.
> > 
> 
> 
> Index: rm.c
> ===
> RCS file: /cvs/src/bin/rm/rm.c,v
> retrieving revision 1.44
> diff -u -p -u -r1.44 rm.c
> --- rm.c  16 Aug 2022 13:52:41 -  1.44
> +++ rm.c  14 Oct 2022 21:41:22 -
> @@ -215,8 +215,11 @@ rm_tree(char **argv)
>   case FTS_F:
>   case FTS_NSOK:
>   if (Pflag)
> - rm_overwrite(p->fts_accpath, p->fts_info ==
> - FTS_NSOK ? NULL : p->fts_statp);
> + if (!rm_overwrite(p->fts_accpath, p->fts_info ==
> + FTS_NSOK ? NULL : p->fts_statp)) {
> + eval = 1;
> + continue;
> + }
>   /* FALLTHROUGH */
>   default:
>   if (!unlink(p->fts_accpath)) {
> @@ -267,7 +270,10 @@ rm_file(char **argv)
>   rval = rmdir(f);
>   else {
>   if (Pflag)
> - rm_overwrite(f, );
> + if (!rm_overwrite(f, )) {
> + eval = 1;
> + continue;
> + }
>   rval = unlink(f);
>   }
>   if (rval && (!fflag || errno != ENOENT)) {
> 
> 
> -- 
> Regards,
>  Mikolaj
> 



Re: rm -P and no-write on files - perm denied, bail out?

2022-10-14 Thread Mikolaj Kucharski
Hi,

Kind reminder. Diff re-attached at the end and on MARC:

https://marc.info/?l=openbsd-tech=166219807307308=2


On Sat, Sep 03, 2022 at 09:44:46AM +, Mikolaj Kucharski wrote:
> Hi,
> 
> I wanted to rm -rP some files on my disk and didn't notice that
> they lacked write permission for the user who executed rm(1)
> command.
> 
> $ echo foo > file-mode-444.txt
> $ chmod 0444 file-mode-444.txt
> $ ls -ln file-mode-444.txt
> -r--r--r--  1 5001  5001  4 Sep  3 09:36 file-mode-444.txt
> 
> $ rm -vfP file-mode-444.txt
> rm: file-mode-444.txt: Permission denied
> file-mode-444.txt
> $ echo $?
> 1
> 
> $ ls -l file-mode-444.txt
> ls: file-mode-444.txt: No such file or directory
> 
> I was not expecting this behaviour. My expectation was the file would
> NOT be removed. Hence the diff below:
> 
> 
> Index: rm.c
> ===
> RCS file: /cvs/src/bin/rm/rm.c,v
> retrieving revision 1.44
> diff -u -p -u -r1.44 rm.c
> --- rm.c  16 Aug 2022 13:52:41 -  1.44
> +++ rm.c  3 Sep 2022 09:37:44 -
> @@ -215,8 +215,11 @@ rm_tree(char **argv)
>   case FTS_F:
>   case FTS_NSOK:
>   if (Pflag)
> - rm_overwrite(p->fts_accpath, p->fts_info ==
> - FTS_NSOK ? NULL : p->fts_statp);
> + if (!rm_overwrite(p->fts_accpath, p->fts_info ==
> + FTS_NSOK ? NULL : p->fts_statp)) {
> + eval = 1;
> + continue;
> + }
>   /* FALLTHROUGH */
>   default:
>   if (!unlink(p->fts_accpath)) {
> @@ -267,7 +270,10 @@ rm_file(char **argv)
>   rval = rmdir(f);
>   else {
>   if (Pflag)
> - rm_overwrite(f, );
> + if (!rm_overwrite(f, )) {
> + eval = 1;
> + continue;
> + }
>   rval = unlink(f);
>   }
>   if (rval && (!fflag || errno != ENOENT)) {
> 
> 
> What do you guys think?
> 
> 
> $ ./obj/rm -vfP file-mode-444.txt
> rm: file-mode-444.txt: Permission denied
> $ echo $?
> 1
> 
> $ ls -ln file-mode-444.txt
> -r--r--r--  1 5001  5001  4 Sep  3 09:36 file-mode-444.txt
> 
> I did use `rm -fP` in the invocation, and reading the rm(1) manual page:
> 
>-f  Attempt to remove the files without prompting for confirmation,
>regardless of the file's permissions.  If the file does not
>exist, do not display a diagnostic message or modify the exit
>status to reflect an error.  The -f option overrides any previous
>-i options.
> 
> but not sure then what exactly should happen when -P and -f and no write
> permission.
> 


Index: rm.c
===
RCS file: /cvs/src/bin/rm/rm.c,v
retrieving revision 1.44
diff -u -p -u -r1.44 rm.c
--- rm.c16 Aug 2022 13:52:41 -  1.44
+++ rm.c14 Oct 2022 21:41:22 -
@@ -215,8 +215,11 @@ rm_tree(char **argv)
case FTS_F:
case FTS_NSOK:
if (Pflag)
-   rm_overwrite(p->fts_accpath, p->fts_info ==
-   FTS_NSOK ? NULL : p->fts_statp);
+   if (!rm_overwrite(p->fts_accpath, p->fts_info ==
+   FTS_NSOK ? NULL : p->fts_statp)) {
+   eval = 1;
+   continue;
+   }
/* FALLTHROUGH */
default:
if (!unlink(p->fts_accpath)) {
@@ -267,7 +270,10 @@ rm_file(char **argv)
rval = rmdir(f);
else {
if (Pflag)
-   rm_overwrite(f, );
+   if (!rm_overwrite(f, )) {
+   eval = 1;
+   continue;
+   }
rval = unlink(f);
}
if (rval && (!fflag || errno != ENOENT)) {


-- 
Regards,
 Mikolaj



rm -P and no-write on files - perm denied, bail out?

2022-09-03 Thread Mikolaj Kucharski
Hi,

I wanted to rm -rP some files on my disk and didn't notice that
they lacked write permission for the user who executed rm(1)
command.

$ echo foo > file-mode-444.txt
$ chmod 0444 file-mode-444.txt
$ ls -ln file-mode-444.txt
-r--r--r--  1 5001  5001  4 Sep  3 09:36 file-mode-444.txt

$ rm -vfP file-mode-444.txt
rm: file-mode-444.txt: Permission denied
file-mode-444.txt
$ echo $?
1

$ ls -l file-mode-444.txt
ls: file-mode-444.txt: No such file or directory

I was not expecting this behaviour. My expectation was the file would
NOT be removed. Hence the diff below:


Index: rm.c
===
RCS file: /cvs/src/bin/rm/rm.c,v
retrieving revision 1.44
diff -u -p -u -r1.44 rm.c
--- rm.c16 Aug 2022 13:52:41 -  1.44
+++ rm.c3 Sep 2022 09:37:44 -
@@ -215,8 +215,11 @@ rm_tree(char **argv)
case FTS_F:
case FTS_NSOK:
if (Pflag)
-   rm_overwrite(p->fts_accpath, p->fts_info ==
-   FTS_NSOK ? NULL : p->fts_statp);
+   if (!rm_overwrite(p->fts_accpath, p->fts_info ==
+   FTS_NSOK ? NULL : p->fts_statp)) {
+   eval = 1;
+   continue;
+   }
/* FALLTHROUGH */
default:
if (!unlink(p->fts_accpath)) {
@@ -267,7 +270,10 @@ rm_file(char **argv)
rval = rmdir(f);
else {
if (Pflag)
-   rm_overwrite(f, );
+   if (!rm_overwrite(f, )) {
+   eval = 1;
+   continue;
+   }
rval = unlink(f);
}
if (rval && (!fflag || errno != ENOENT)) {


What do you guys think?


$ ./obj/rm -vfP file-mode-444.txt
rm: file-mode-444.txt: Permission denied
$ echo $?
1

$ ls -ln file-mode-444.txt
-r--r--r--  1 5001  5001  4 Sep  3 09:36 file-mode-444.txt

I did use `rm -fP` in the invocation, and reading the rm(1) manual page:

   -f  Attempt to remove the files without prompting for confirmation,
   regardless of the file's permissions.  If the file does not
   exist, do not display a diagnostic message or modify the exit
   status to reflect an error.  The -f option overrides any previous
   -i options.

but not sure then what exactly should happen when -P and -f and no write
permission.


-- 
Regards,
 Mikolaj