Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [PATCH 1/2] Introduce new completion for Linux task capabilities

On Sat, Feb 27, 2021 at 01:03:53PM +0100, Oliver Kiddle wrote:
> Arseny Maslennikov wrote:
> > On Fri, Feb 26, 2021 at 03:50:18PM +0000, Daniel Shahaf wrote:
> > > Arseny Maslennikov wrote on Fri, Feb 26, 2021 at 10:55:57 +0300:
> > > > The next patch introduces a completion for setpriv(1), which actively
> Thanks for the contribution. I've reviewed the second patch that Daniel
> left though in general it all looks very good and there isn't much need
> to comment.
> > > > uses _capability_names. As for _capabilities,
> Are these POSIX capabilities?

The corresponding POSIX draft standard was actually withdrawn.

Even if we imagine it wasn't, however, as time passes, new capabilities
are regularly being introduced to Linux, and I'm not sure if POSIX
capabilities would reflect the additions soon enough for the supported
capability sets to not diverge significantly. It's not like Linux
development follows POSIX, but the other way around — the bits that make
their way to popular Unix-like systems are codified as parts of the
POSIX standard.

> The term capabilities gets wider usage so
> I'd be inclined to call the function _posix_capabilities despite that
> being longer. As far as I can tell, unless you count Hurd, Linux is the
> only implementation of POSIX capabilities so it is fine for this
> to be in the Linux directory. That said, I'm not sure I would factor it
> out of _setpriv until there's at least two users of it.

After pondering this question, I get your point. I'm now inclined to
agree with you on the _posix_capabilities name, since it clearly conveys
what kind of capabilities the completion function is about, if and only
if the aforementioned POSIX draft is resurrected in some way.

If «_capabilities» is still too unclear, I suggest «_task_capabilities».

> > > FWIW, I think it may well be possible to write _setpriv in terms of
> > > _capabilities as a black box, using some combination of _sequence,
> > > matchspecs, «compadd -P», et al..
> >
> > Do you mean invoking «_capabilities -O array_name» to pass the -O to
> > compadd, and then using the array_name in _setpriv and possible future
> > users?
> > As far as I understand, in that case _capabilities will have to avoid
> > looping over tag labels, i. e. calling _wanted, as it does now.
> No. You would call _capabilities with compadd style options to deal with
> the prefixes. You have the following:
>   matches=( {-,+}"${(@)^caps#cap_}" )
> So _capabilities is unsuited to _setpriv because you don't want the
> cap_ prefix but do want - and + prefixes. Telling compadd/completion
> functions to strip a prefix is somewhat messy, you can get close with
> matching control (-M 'B:=cap_') but it continues to prefer the prefix
> to be present. So I'd suggest that you chop off the initial "cap_" from
> everything _capabilites completes.
> Adding prefixes is easier. If some other function needs capabilities
> with a cap_ prefix, that can then call it with -p 'cap_',
> For the - and +, we can also add these as a prefix but in their case, -P
> is more appropriate because the prefix is a separate thing and it may
> even be better to strip them with compset and complete them
> independently so that we can indicate that they are for removing and
> adding capabilities.

OK, I managed to make this work with «compset -P [+-]» and eliminate the
need for _capability_names; I'm going to send the revised patch shortly.
Thanks a lot for the thorough explanation!

> > > > +if [[ $OSTYPE != linux* ]]; then
> > > > +    _default; return
> > > > +fi
> > > 
> > > Why?  The code doesn't use /proc or /sbin/some-linux-only-tool, and
> > > having this guard prevents people on other OSTYPE's from completing «ssh
> > > $linuxbox foo --with-capability=<TAB>».
> >
> > I intended to play nice in case unrelated "capabilities" are present (or
> > introduced later) on those OSTYPE's and pulled Oliver's trick from the
> > recently posted _unshare. There might be a better way to do it; I'm open
> > to discussion.
> Not sure I'd bother in this case as - given the lack of setpriv commands
> on other operating systems - it is unlikely to be called other than in
> a case like the ssh one Daniel mentions. It mattered in the case of
> unshare because unshare does exist on other OSes as an unrelated command
> to remove NFS exports.

OK, I'll remove that check until this is a problem.

> > > Suggest to list these one per line, because:
> > I'd personally like them being listed one per line, too. Will do.
> I'd certainly avoid exceeding 80 columns unnecessarily but if it ends up
> using a lot of vertical space, grouping similar ones or by first letter
> is also fine.
> Arseny Maslennikov [patch 2/2] wrote:
> > +__setpriv_prctl_securebits_set_element() {
> I wouldn't bother with the double-underscore prefix. _git did that and
> seems to have been copied elsewhere but it doesn't really serve much
> purpose. I think with git it was done to make a more obvious distinction
> to subcommands than just whether it was _git_… or _git-…

I wanted that prefix to denote a quasi-namespace of sorts, since zsh
does not have a concept of local functions. E. g.:
* _setpriv is a "front-end" completion function, as is _git, _signals, ...
  One underscore.
* __setpriv_death_signals is an implementation detail of _setpriv that
  completes a death signal and depends on how it is called (is it
  expected to call _description? is it expected to loop over labels?), so
  it has _setpriv directly in the name after the initial underscore.

Something like this was my initial understanding of how _git and all the
functions that followed suit use __.

> I'd be inclined to just name it _setpriv_prctl_securebits as that is
> what it appears to complete.

This would be an appropriate name if it generated matches from the bits
array, i. e. the securebit names themselves as matches. Instead, the
function has to append '-' or '+' to each securebit name.

> > +  bits=(noroot noroot_locked
> > +        no_setuid_fixup no_setuid_fixup_locked
> > +  _wanted minus-plus-securebits expl 'prctl securebits' \
> > +    compadd "$@" -a - matches
> Minor nitpick but Etc/completion-style-guide states four spaces of
> indentation for continuation lines. My own personal taste includes a
> particular dislike for the variable indentation that results from lining
> things up with the syntactic construct on the previous line as in the
> bits=( assignment.
> Also, the description should be in the singular ('prctl securebit').
> Yes, there may be multiple matches, and we may be in the middle of a
> comma-separated list of them but only one is completed at a time.

OK, I'm going to fix all the description strings to be in the singular.
To be fair, in the wild and likely even in the completion functions
bundled with zsh I've seen both plural and singular forms there.

> T'd be sufficient to use _description followed by compadd rather than
> _wanted. There are aspects of nested tag loops that aren't ideal so
> when you can know that completion is already within one tag loop (which
> _arguments does), we don't need another one.
> > +__setpriv_prctl_securebits_set() {
> > +  _sequence __setpriv_prctl_securebits_set_element
> > +}
> You can call _sequence directly from the _arguments spec. I'd probably
> just do that rather than having a separate function. Convention for

I do not really have a preference for separate functions or inlining
calls/matches in the optspec. Separate function names, though, are
particularly easy to locate in the xtraced output generated by
_complete_debug, and they're often shorter than the one-liner they
implement so the separate name makes the optspec line easier to fit in
80 cols or whatever's the limit, so they got that going for them.

> separate functions is a plural name, i.e. …_sets

So «: : _sequence __setpriv_prctl_securebit_set_elements». That's all
right to me, but the full line for --securebits would be 97 columns
The longest line is 105, so not really a problem, I guess...

> > +__setpriv_caps_all() {
> > +  # Nonlocal expl; _description call expected.
> Is that necessary? Aren't descriptions passed through with "$@"?

I checked with _complete_debug; they indeed are.

> > +__setpriv_cap_set_element() {
> > +  # We pass through arguments added by _sequence.
> > +  local -a Oargv=( "$@" )
> > +  _alternative -O Oargv \
> > +    'special-actions:drop/obtain all caps:__setpriv_caps_all' \
> Wouldn't :(-all +all) do for the action here instead of needing another
> separate function.
> There probably ought to be a nicer way to add special all/any elements
> to a list because it comes up very often.
> > +    'minus-plus-caps:capabilities:__setpriv_capability_expressions' \
> Again, use singular form for the description.
> > +local context state state_descr line
> > +typeset -A opt_args
> Given that you've not used states, these are superfluous.
> > +  '(- : *)*'{-d,--dump}'[display the current privilege state]' \
> The exclusion list here breaks the repeatability.
> I did once look into making a special case for this in _arguments but it
> wasn't trivial.

The cleanest way to fix this that comes to my mind is to introduce a
state here and call
    _arguments '*'{-d,--dump}'[display the current privilege state]'
from the handler.
I'll send that as a separate patch in the series.

> > +  '--clear-groups[clear supplementary groups]' \
> > +  '--groups[set supplementary groups]:groups:_groups' \
> How do you specify multiple supplementary groups? If --groups is
> repeated, we need * at the beginning of the spec. Otherwise, is
> _sequence needed.

I forgot to use _sequence there.

> Are there any mutual exclusions between it and --clear-groups,
> --keep-groups and --init-groups.

Turns out they all mutually exclude each other. Added.

> > +  '--inh-caps[set inheritable caps]:capability set: __setpriv_cap_set' \
> > +  '--ambient-caps[set ambient caps]:capability set: __setpriv_cap_set' \
> Where the _arguments descriptions are being thrown out anyway, it may be
> better to leave them out entirely and just have a single space.

Done everywhere in the code.

> > +  '--securebits[set "process securebits"]:prctl securebits:__setpriv_prctl_securebits_set' \
> Starting with this one is a run of plural descriptions.

Attachment: signature.asc
Description: PGP signature

Messages sorted by: Reverse Date, Date, Thread, Author