5.6.7-rc1 review
Subsystems (8)
Greg Kroah-Hartman
maintainerPATCH 162/166 KEYS: Dont write out to userspace while holding key semaphore
In reply to Greg Kroah-HartmanFrom: Waiman Long <[email protected]>
commit d3ec10aa95819bff18a0d936b18884c7816d0914 upstream.
A lockdep circular locking dependency report was seen when running a keyutils test:
[12537.027242] ======================================================
[12537.059309] WARNING: possible circular locking dependency detected
[12537.088148] 4.18.0-147.7.1.el8_1.x86_64+debug #1 Tainted: G OE --------- - -
[12537.125253] ------------------------------------------------------
[12537.153189] keyctl/25598 is trying to acquire lock:
[12537.175087] 000000007c39f96c (&mm->mmap_sem){++++}, at: __might_fault+0xc4/0x1b0
[12537.208365]
[12537.208365] but task is already holding lock:
[12537.234507] 000000003de5b58d (&type->lock_class){++++}, at: keyctl_read_key+0x15a/0x220
[12537.270476]
[12537.270476] which lock already depends on the new lock.
[12537.270476]
[12537.307209]
[12537.307209] the existing dependency chain (in reverse order) is:
[12537.340754]
[12537.340754] -> #3 (&type->lock_class){++++}:
[12537.367434] down_write+0x4d/0x110
[12537.385202] __key_link_begin+0x87/0x280
[12537.405232] request_key_and_link+0x483/0xf70
[12537.427221] request_key+0x3c/0x80
[12537.444839] dns_query+0x1db/0x5a5 [dns_resolver]
[12537.468445] dns_resolve_server_name_to_ip+0x1e1/0x4d0 [cifs]
[12537.496731] cifs_reconnect+0xe04/0x2500 [cifs]
[12537.519418] cifs_readv_from_socket+0x461/0x690 [cifs]
[12537.546263] cifs_read_from_socket+0xa0/0xe0 [cifs]
[12537.573551] cifs_demultiplex_thread+0x311/0x2db0 [cifs]
[12537.601045] kthread+0x30c/0x3d0
[12537.617906] ret_from_fork+0x3a/0x50
[12537.636225]
[12537.636225] -> #2 (root_key_user.cons_lock){+.+.}:
[12537.664525] __mutex_lock+0x105/0x11f0
[12537.683734] request_key_and_link+0x35a/0xf70
[12537.705640] request_key+0x3c/0x80
[12537.723304] dns_query+0x1db/0x5a5 [dns_resolver]
[12537.746773] dns_resolve_server_name_to_ip+0x1e1/0x4d0 [cifs]
[12537.775607] cifs_reconnect+0xe04/0x2500 [cifs]
[12537.798322] cifs_readv_from_socket+0x461/0x690 [cifs]
[12537.823369] cifs_read_from_socket+0xa0/0xe0 [cifs]
[12537.847262] cifs_demultiplex_thread+0x311/0x2db0 [cifs]
[12537.873477] kthread+0x30c/0x3d0
[12537.890281] ret_from_fork+0x3a/0x50
[12537.908649]
[12537.908649] -> #1 (&tcp_ses->srv_mutex){+.+.}:
[12537.935225] __mutex_lock+0x105/0x11f0
[12537.954450] cifs_call_async+0x102/0x7f0 [cifs]
[12537.977250] smb2_async_readv+0x6c3/0xc90 [cifs]
[12538.000659] cifs_readpages+0x120a/0x1e50 [cifs]
[12538.023920] read_pages+0xf5/0x560
[12538.041583] __do_page_cache_readahead+0x41d/0x4b0
[12538.067047] ondemand_readahead+0x44c/0xc10
[12538.092069] filemap_fault+0xec1/0x1830
[12538.111637] __do_fault+0x82/0x260
[12538.129216] do_fault+0x419/0xfb0
[12538.146390] __handle_mm_fault+0x862/0xdf0
[12538.167408] handle_mm_fault+0x154/0x550
[12538.187401] __do_page_fault+0x42f/0xa60
[12538.207395] do_page_fault+0x38/0x5e0
[12538.225777] page_fault+0x1e/0x30
[12538.243010]
[12538.243010] -> #0 (&mm->mmap_sem){++++}:
[12538.267875] lock_acquire+0x14c/0x420
[12538.286848] __might_fault+0x119/0x1b0
[12538.306006] keyring_read_iterator+0x7e/0x170
[12538.327936] assoc_array_subtree_iterate+0x97/0x280
[12538.352154] keyring_read+0xe9/0x110
[12538.370558] keyctl_read_key+0x1b9/0x220
[12538.391470] do_syscall_64+0xa5/0x4b0
[12538.410511] entry_SYSCALL_64_after_hwframe+0x6a/0xdf
[12538.435535]
[12538.435535] other info that might help us debug this:
[12538.435535]
[12538.472829] Chain exists of:
[12538.472829] &mm->mmap_sem --> root_key_user.cons_lock --> &type->lock_class
[12538.472829]
[12538.524820] Possible unsafe locking scenario:
[12538.524820]
[12538.551431] CPU0 CPU1
[12538.572654] ---- ----
[12538.595865] lock(&type->lock_class);
[12538.613737] lock(root_key_user.cons_lock);
[12538.644234] lock(&type->lock_class);
[12538.672410] lock(&mm->mmap_sem);
[12538.687758]
[12538.687758] *** DEADLOCK ***
[12538.687758]
[12538.714455] 1 lock held by keyctl/25598:
[12538.732097] #0: 000000003de5b58d (&type->lock_class){++++}, at: keyctl_read_key+0x15a/0x220
[12538.770573]
[12538.770573] stack backtrace:
[12538.790136] CPU: 2 PID: 25598 Comm: keyctl Kdump: loaded Tainted: G
[12538.844855] Hardware name: HP ProLiant DL360 Gen9/ProLiant DL360 Gen9, BIOS P89 12/27/2015
[12538.881963] Call Trace:
[12538.892897] dump_stack+0x9a/0xf0
[12538.907908] print_circular_bug.isra.25.cold.50+0x1bc/0x279
[12538.932891] ? save_trace+0xd6/0x250
[12538.948979] check_prev_add.constprop.32+0xc36/0x14f0
[12538.971643] ? keyring_compare_object+0x104/0x190
[12538.992738] ? check_usage+0x550/0x550
[12539.009845] ? sched_clock+0x5/0x10
[12539.025484] ? sched_clock_cpu+0x18/0x1e0
[12539.043555] __lock_acquire+0x1f12/0x38d0
[12539.061551] ? trace_hardirqs_on+0x10/0x10
[12539.080554] lock_acquire+0x14c/0x420
[12539.100330] ? __might_fault+0xc4/0x1b0
[12539.119079] __might_fault+0x119/0x1b0
[12539.135869] ? __might_fault+0xc4/0x1b0
[12539.153234] keyring_read_iterator+0x7e/0x170
[12539.172787] ? keyring_read+0x110/0x110
[12539.190059] assoc_array_subtree_iterate+0x97/0x280
[12539.211526] keyring_read+0xe9/0x110
[12539.227561] ? keyring_gc_check_iterator+0xc0/0xc0
[12539.249076] keyctl_read_key+0x1b9/0x220
[12539.266660] do_syscall_64+0xa5/0x4b0
[12539.283091] entry_SYSCALL_64_after_hwframe+0x6a/0xdfOne way to prevent this deadlock scenario from happening is to not allow writing to userspace while holding the key semaphore. Instead, an internal buffer is allocated for getting the keys out from the read method first before copying them out to userspace without holding the lock.
That requires taking out the __user modifier from all the relevant read methods as well as additional changes to not use any userspace write helpers. That is,
1) The put_user() call is replaced by a direct copy.
2) The copy_to_user() call is replaced by memcpy().
3) All the fault handling code is removed.
Compiling on a x86-64 system, the size of the rxrpc_read() function is reduced from 3795 bytes to 2384 bytes with this patch.
--- include/keys/big_key-type.h | 2 include/keys/user-type.h | 3 - include/linux/key-type.h | 2 net/dns_resolver/dns_key.c | 2 net/rxrpc/key.c | 27 +++-------- security/keys/big_key.c | 11 +--- security/keys/encrypted-keys/encrypted.c | 7 +- security/keys/keyctl.c | 73 +++++++++++++++++++++++------- security/keys/keyring.c | 6 -- security/keys/request_key_auth.c | 7 +- security/keys/trusted-keys/trusted_tpm1.c | 14 ----- security/keys/user_defined.c | 5 -- 12 files changed, 85 insertions(+), 74 deletions(-)
include/keys/big_key-type.h +1 -1
@@ -17,6 +17,6 @@ extern void big_key_free_preparse(structextern void big_key_revoke (struct key *key);extern void big_key_destroy (struct key *key);extern void big_key_describe (const struct key *big_key,struct seq_file *m); -extern long big_key_read (const struct key *key,char __user *buffer,size_t buflen); +extern long big_key_read (const struct key *key,char *buffer,size_t buflen);#endif /* _KEYS_BIG_KEY_TYPE_H */
include/keys/user-type.h +1 -2
@@ -41,8 +41,7 @@ extern int user_update(struct key *key,extern void user_revoke (struct key *key);extern void user_destroy (struct key *key);extern void user_describe (const struct key *user,struct seq_file *m); -extern long user_read (const struct key *key, -char __user *buffer,size_t buflen); +extern long user_read (const struct key *key,char *buffer,size_t buflen);static inline const struct user_key_payload *user_key_payload_rcu (const struct key *key) {
include/linux/key-type.h +1 -1
@@ -127,7 +127,7 @@ struct key_type {* much is copied into the buffer * - shouldn't do the copy if the buffer is NULL */ -long (*read)(const struct key *key,char __user *buffer,size_t buflen); +long (*read)(const struct key *key,char *buffer,size_t buflen);/* handle request_key() for this type instead of invoking * /sbin/request-key (optional)
net/dns_resolver/dns_key.c +1 -1
@@ -302,7 +302,7 @@ static void dns_resolver_describe(const* - the key's semaphore is read-locked */ static long dns_resolver_read (const struct key *key, -char __user *buffer,size_t buflen) +char *buffer,size_t buflen) {int err =PTR_ERR (key->payload.data[dns_key_error]);
net/rxrpc/key.c +9 -18
@@ -31,7 +31,7 @@ static void rxrpc_free_preparse_s(structstatic void rxrpc_destroy (struct key *);static void rxrpc_destroy_s (struct key *);static void rxrpc_describe (const struct key *,struct seq_file *); -static long rxrpc_read (const struct key *,char __user *,size_t ); +static long rxrpc_read (const struct key *,char *,size_t );/* * rxrpc defined keys take an arbitrary string as the description and an
@@ -1042,12 +1042,12 @@ EXPORT_SYMBOL(rxrpc_get_null_key);* - this returns the result in XDR form */ static long rxrpc_read (const struct key *key, -char __user *buffer,size_t buflen) +char *buffer,size_t buflen) {const struct rxrpc_key_token *token;const struct krb5_principal *princ;size_t size; -__be32 __user *xdr, *oldxdr; +__be32 *xdr, *oldxdr;u32 cnlen, toksize, ntoks, tok, zero;u16 toksizes[AFSTOKEN_MAX ];int loop;
@@ -1124,30 +1124,25 @@ static long rxrpc_read(const struct keyif (!buffer || buflen < size)return size; - xdr = (__be32 __user *) buffer; + xdr = (__be32 *)buffer; zero =0 ;#define ENCODE (x) \ do { \ - __be32 y = htonl(x); \ - if (put_user(y, xdr++) < 0) \ - goto fault; \ + *xdr++ = htonl(x); \ } while(0)#define ENCODE_DATA (l, s) \ do { \ u32 _l = (l); \ ENCODE(l); \ - if (copy_to_user(xdr, (s), _l) != 0) \ - goto fault; \ - if (_l & 3 && \ - copy_to_user((u8 __user *)xdr + _l, &zero, 4 - (_l & 3)) != 0) \ - goto fault; \ + memcpy(xdr, (s), _l); \ + if (_l & 3) \ + memcpy((u8 *)xdr + _l, &zero, 4 - (_l & 3)); \ xdr += (_l + 3) >> 2; \ } while(0)#define ENCODE64 (x) \ do { \ __be64 y = cpu_to_be64(x); \ - if (copy_to_user(xdr, &y, 8) != 0) \ - goto fault; \ + memcpy(xdr, &y, 8); \ xdr += 8 >> 2; \ } while(0)#define ENCODE_STR (s) \
@@ -1238,8 +1233,4 @@ static long rxrpc_read(const struct key ASSERTCMP((char __user *) xdr - buffer, ==, size);_leave (" = %zu" , size);return size; - -fault: -_leave (" = -EFAULT" ); -return -EFAULT ; }
security/keys/big_key.c +4 -7
@@ -352,7 +352,7 @@ void big_key_describe(const struct key ** read the key data * - the key's semaphore is read-locked */ -long big_key_read (const struct key *key,char __user *buffer,size_t buflen) +long big_key_read (const struct key *key,char *buffer,size_t buflen) {size_t datalen = (size_t )key->payload.data[big_key_len];long ret;
@@ -391,9 +391,8 @@ long big_key_read(const struct key *key, ret = datalen; -/* copy decrypted data to user */ -if (copy_to_user (buffer, buf->virt, datalen) !=0 ) - ret = -EFAULT ; +/* copy out decrypted data */ +memcpy (buffer, buf->virt, datalen); err_fput:fput (file);
@@ -401,9 +400,7 @@ error:big_key_free_buffer (buf); }else { ret = datalen; -if (copy_to_user (buffer, key->payload.data[big_key_data], - datalen) !=0 ) - ret = -EFAULT ; +memcpy (buffer, key->payload.data[big_key_data], datalen); }return ret;
security/keys/encrypted-keys/encrypted.c +3 -4
@@ -902,14 +902,14 @@ out: }/* -* encrypted_read - format and copy the encrypted data to userspace +* encrypted_read - format and copy out the encrypted data * * The resulting datablob format is: * <master-key name> <decrypted data length> <encrypted iv> <encrypted data> * * On success, return to userspace the encrypted key datablob size. */ -static long encrypted_read (const struct key *key,char __user *buffer, +static long encrypted_read (const struct key *key,char *buffer,size_t buflen) {struct encrypted_key_payload *epayload;
@@ -957,8 +957,7 @@ static long encrypted_read(const structkey_put (mkey);memzero_explicit (derived_key,sizeof (derived_key)); -if (copy_to_user (buffer, ascii_buf, asciiblob_len) !=0 ) - ret = -EFAULT ; +memcpy (buffer, ascii_buf, asciiblob_len);kzfree (ascii_buf);return asciiblob_len;
security/keys/keyctl.c +57 -16
115 lines not on this page · raw message
security/keys/keyring.c +1 -5
@@ -459,7 +459,6 @@ static int keyring_read_iterator(const v {struct keyring_read_iterator_context *ctx = data;const struct key *key =keyring_ptr_to_key (object); -int ret;kenter ("{%s,%d},,{%zu/%zu}" , key->type->name, key->serial, ctx->count, ctx->buflen);
@@ -467,10 +466,7 @@ static int keyring_read_iterator(const vif (ctx->count >= ctx->buflen)return 1 ; - ret =put_user (key->serial, ctx->buffer); -if (ret <0 ) -return ret; - ctx->buffer++; + *ctx->buffer++ = key->serial; ctx->count +=sizeof (key->serial);return 0 ; }
security/keys/request_key_auth.c +3 -4
@@ -22,7 +22,7 @@ static int request_key_auth_instantiate(static void request_key_auth_describe (const struct key *,struct seq_file *);static void request_key_auth_revoke (struct key *);static void request_key_auth_destroy (struct key *); -static long request_key_auth_read (const struct key *,char __user *,size_t ); +static long request_key_auth_read (const struct key *,char *,size_t );/* * The request-key authorisation key type definition.
@@ -80,7 +80,7 @@ static void request_key_auth_describe(co* - the key's semaphore is read-locked */ static long request_key_auth_read (const struct key *key, -char __user *buffer,size_t buflen) +char *buffer,size_t buflen) {struct request_key_auth *rka =dereference_key_locked (key);size_t datalen;
@@ -97,8 +97,7 @@ static long request_key_auth_read(constif (buflen > datalen) buflen = datalen; -if (copy_to_user (buffer, rka->callout_info, buflen) !=0 ) - ret = -EFAULT ; +memcpy (buffer, rka->callout_info, buflen); }return ret;
security/keys/trusted-keys/trusted_tpm1.c +2 -12
@@ -1130,11 +1130,10 @@ out:* trusted_read - copy the sealed blob data to userspace in hex. * On success, return to userspace the trusted key datablob size. */ -static long trusted_read (const struct key *key,char __user *buffer, +static long trusted_read (const struct key *key,char *buffer,size_t buflen) {const struct trusted_key_payload *p; -char *ascii_buf;char *bufp;int i;
@@ -1143,18 +1142,9 @@ static long trusted_read(const struct kereturn -EINVAL ;if (buffer && buflen >=2 * p->blob_len) { - ascii_buf =kmalloc_array (2 , p->blob_len,GFP_KERNEL ); -if (!ascii_buf) -return -ENOMEM ; - - bufp = ascii_buf; + bufp = buffer;for (i =0 ; i < p->blob_len; i++) bufp =hex_byte_pack (bufp, p->blob[i]); -if (copy_to_user (buffer, ascii_buf,2 * p->blob_len) !=0 ) { -kzfree (ascii_buf); -return -EFAULT ; - } -kzfree (ascii_buf); }return 2 * p->blob_len; }
security/keys/user_defined.c +2 -3
@@ -168,7 +168,7 @@ EXPORT_SYMBOL_GPL(user_describe);* read the key data * - the key's semaphore is read-locked */ -long user_read (const struct key *key,char __user *buffer,size_t buflen) +long user_read (const struct key *key,char *buffer,size_t buflen) {const struct user_key_payload *upayload;long ret;
@@ -181,8 +181,7 @@ long user_read(const struct key *key, chif (buflen > upayload->datalen) buflen = upayload->datalen; -if (copy_to_user (buffer, upayload->data, buflen) !=0 ) - ret = -EFAULT ; +memcpy (buffer, upayload->data, buflen); }return ret;
Greg Kroah-Hartman
maintainerPATCH 163/166 bpf: fix buggy r0 retval refinement for tracing helpers
In reply to Greg Kroah-HartmanFrom: Daniel Borkmann <[email protected]>
Date: Tue, 21 Apr 2020 15:01:49 +0200
[ no upstream commit ]
See the glory details in 100605035e15 ("bpf: Verifier, do_refine_retval_range may clamp umin to 0 incorrectly") for why 849fa50662fb ("bpf/verifier: refine retval R0 state for bpf_get_stack helper") is buggy. The whole series however is not suitable for stable since it adds significant amount [0] of verifier complexity in order to add 32bit subreg tracking. Something simpler is needed.
Unfortunately, reverting 849fa50662fb ("bpf/verifier: refine retval R0 state for bpf_get_stack helper") or just cherry-picking 100605035e15 ("bpf: Verifier, do_refine_retval_range may clamp umin to 0 incorrectly") is not an option since it will break existing tracing programs badly (at least those that are using bpf_get_stack() and bpf_probe_read_str() helpers). Not fixing it in stable is also not an option since on 4.19 kernels an error will cause a soft-lockup due to hitting dead-code sanitized branch since we don't hard-wire such branches in old kernels yet. But even then for 5.x 849fa50662fb ("bpf/verifier: refine retval R0 state for bpf_get_stack helper") would cause wrong bounds on the verifier simluation when an error is hit.
In one of the earlier iterations of mentioned patch series for upstream there was the concern that just using smax_value in do_refine_retval_range() would nuke bounds by subsequent <<32 >>32 shifts before the comparison against 0 [1] which eventually led to the 32bit subreg tracking in the first place. While I initially went for implementing the idea [1] to pattern match the two shift operations, it turned out to be more complex than actually needed, meaning, we could simply treat do_refine_retval_range() similarly to how we branch off verification for conditionals or under speculation, that is, pushing a new reg state to the stack for later verification. This means, instead of verifying the current path with the ret_reg in [S32MIN, msize_max_value] interval where later bounds would get nuked, we split this into two: i) for the success case where ret_reg can be in [0, msize_max_value], and ii) for the error case with ret_reg known to be in interval [S32MIN, -1]. Latter will preserve the bounds during these shift patterns and can match reg < 0 test. test_progs also succeed with this approach.
[0] https://lore.kernel.org/bpf/158507130343.15666.8018068546764556975.stgit@john-Precision-5820-Tower/
[1] https://lore.kernel.org/bpf/158015334199.28573.4940395881683556537.stgit@john-XPS-13-9370/T/#m2e0ad1d5949131014748b6daa48a3495e7f0456d
--- kernel/bpf/verifier.c | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-)
kernel/bpf/verifier.c +34 -11
@@ -227,8 +227,7 @@ struct bpf_call_arg_meta {bool pkt_access;int regno;int access_size; -s64 msize_smax_value; -u64 msize_umax_value; +u64 msize_max_value;int ref_obj_id;int func_id;u32 btf_id;
@@ -3568,8 +3567,7 @@ static int check_func_arg(struct bpf_ver/* remember the mem_size which may be used later * to refine return values. */ - meta->msize_smax_value = reg->smax_value; - meta->msize_umax_value = reg->umax_value; + meta->msize_max_value = reg->umax_value;/* The register is SCALAR_VALUE; the access check * happens using its boundaries.
@@ -4095,21 +4093,44 @@ static int prepare_func_exit(struct bpf_return 0 ; } -static void do_refine_retval_range (struct bpf_reg_state *regs,int ret_type, -int func_id, -struct bpf_call_arg_meta *meta) +static int do_refine_retval_range (struct bpf_verifier_env *env, +struct bpf_reg_state *regs,int ret_type, +int func_id,struct bpf_call_arg_meta *meta) {struct bpf_reg_state *ret_reg = ®s[BPF_REG_0 ]; +struct bpf_reg_state tmp_reg = *ret_reg; +bool ret;if (ret_type !=RET_INTEGER || (func_id != BPF_FUNC_get_stack && func_id != BPF_FUNC_probe_read_str)) -return ; +return 0 ; + +/* Error case where ret is in interval [S32MIN, -1]. */ + ret_reg->smin_value =S32_MIN ; + ret_reg->smax_value =-1 ; - ret_reg->smax_value = meta->msize_smax_value; - ret_reg->umax_value = meta->msize_umax_value;__reg_deduce_bounds (ret_reg);__reg_bound_offset (ret_reg); +__update_reg_bounds (ret_reg); + + ret =push_stack (env, env->insn_idx +1 , env->insn_idx, false); +if (!ret) +return -EFAULT ; + + *ret_reg = tmp_reg; + +/* Success case where ret is in range [0, msize_max_value]. */ + ret_reg->smin_value =0 ; + ret_reg->smax_value = meta->msize_max_value; + ret_reg->umin_value = ret_reg->smin_value; + ret_reg->umax_value = ret_reg->smax_value; + +__reg_deduce_bounds (ret_reg); +__reg_bound_offset (ret_reg); +__update_reg_bounds (ret_reg); + +return 0 ; } static int
@@ -4377,7 +4398,9 @@ static int check_helper_call(struct bpf_ regs[BPF_REG_0 ].ref_obj_id = id; } -do_refine_retval_range (regs, fn->ret_type, func_id, &meta); + err =do_refine_retval_range (env, regs, fn->ret_type, func_id, &meta); +if (err) +return err; err =check_map_func_compatibility (env, meta.map_ptr, func_id);if (err)
Greg Kroah-Hartman
maintainerPATCH 164/166 bpf: Test_verifier, bpf_get_stack return value add <0
In reply to Greg Kroah-HartmanFrom: John Fastabend <[email protected]>
commit 9ac26e9973bac5716a2a542e32f380c84db2b88c upstream.
With current ALU32 subreg handling and retval refine fix from last patches we see an expected failure in test_verifier. With verbose verifier state being printed at each step for clarity we have the following relavent lines [I omit register states that are not necessarily useful to see failure cause],
#101/p bpf_get_stack return R0 within range FAIL Failed to load prog 'Success'!
[..]
14: (85) call bpf_get_stack#67
R0_w=map_value(id=0,off=0,ks=8,vs=48,imm=0) R3_w=inv48
15:
R0=inv(id=0,smax_value=48,var32_off=(0x0; 0xffffffff))
15: (b7) r1 = 0
16:
R0=inv(id=0,smax_value=48,var32_off=(0x0; 0xffffffff)) R1_w=inv0
16: (bf) r8 = r0
17:
R0=inv(id=0,smax_value=48,var32_off=(0x0; 0xffffffff)) R1_w=inv0 R8_w=inv(id=0,smax_value=48,var32_off=(0x0; 0xffffffff))
17: (67) r8 <<= 32
18:
R0=inv(id=0,smax_value=48,var32_off=(0x0; 0xffffffff)) R1_w=inv0 R8_w=inv(id=0,smax_value=9223372032559808512,
umax_value=18446744069414584320,
var_off=(0x0; 0xffffffff00000000),
s32_min_value=0,
s32_max_value=0,
u32_max_value=0,
var32_off=(0x0; 0x0))18: (c7) r8 s>>= 32
19
R0=inv(id=0,smax_value=48,var32_off=(0x0; 0xffffffff)) R1_w=inv0 R8_w=inv(id=0,smin_value=-2147483648,
smax_value=2147483647,
var32_off=(0x0; 0xffffffff))19: (cd) if r1 s< r8 goto pc+16
R0=inv(id=0,smax_value=48,var32_off=(0x0; 0xffffffff)) R1_w=inv0 R8_w=inv(id=0,smin_value=-2147483648,
smax_value=0,
var32_off=(0x0; 0xffffffff))20:
R0=inv(id=0,smax_value=48,var32_off=(0x0; 0xffffffff)) R1_w=inv0 R8_w=inv(id=0,smin_value=-2147483648,
smax_value=0,
R9=inv48
20: (1f) r9 -= r8
21: (bf) r2 = r7
22:
R2_w=map_value(id=0,off=0,ks=8,vs=48,imm=0)
22: (0f) r2 += r8
value -2147483648 makes map_value pointer be out of bounds
After call bpf_get_stack() on line 14 and some moves we have at line 16 an r8 bound with max_value 48 but an unknown min value. This is to be expected bpf_get_stack call can only return a max of the input size but is free to return any negative error in the 32-bit register space. The C helper is returning an int so will use lower 32-bits.
Lines 17 and 18 clear the top 32 bits with a left/right shift but use ARSH so we still have worst case min bound before line 19 of -2147483648. At this point the signed check 'r1 s< r8' meant to protect the addition on line 22 where dst reg is a map_value pointer may very well return true with a large negative number. Then the final line 22 will detect this as an invalid operation and fail the program. What we want to do is proceed only if r8 is positive non-error. So change 'r1 s< r8' to 'r1 s> r8' so that we jump if r8 is negative.
Next we will throw an error because we access past the end of the map value. The map value size is 48 and sizeof(struct test_val) is 48 so we walk off the end of the map value on the second call to get bpf_get_stack(). Fix this by changing sizeof(struct test_val) to 24 by using 'sizeof(struct test_val) / 2'. After this everything passes as expected.
--- tools/testing/selftests/bpf/verifier/bpf_get_stack.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/verifier/bpf_get_stack.c +4 -4
@@ -9,17 +9,17 @@BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , BPF_FUNC_map_lookup_elem),BPF_JMP_IMM (BPF_JEQ ,BPF_REG_0 ,0 ,28 ),BPF_MOV64_REG (BPF_REG_7 ,BPF_REG_0 ), -BPF_MOV64_IMM (BPF_REG_9 ,sizeof (struct test_val )), +BPF_MOV64_IMM (BPF_REG_9 ,sizeof (struct test_val )/2 ),BPF_MOV64_REG (BPF_REG_1 ,BPF_REG_6 ),BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_7 ), -BPF_MOV64_IMM (BPF_REG_3 ,sizeof (struct test_val )), +BPF_MOV64_IMM (BPF_REG_3 ,sizeof (struct test_val )/2 ),BPF_MOV64_IMM (BPF_REG_4 ,256 ),BPF_EMIT_CALL (BPF_FUNC_get_stack),BPF_MOV64_IMM (BPF_REG_1 ,0 ),BPF_MOV64_REG (BPF_REG_8 ,BPF_REG_0 ),BPF_ALU64_IMM (BPF_LSH ,BPF_REG_8 ,32 ),BPF_ALU64_IMM (BPF_ARSH ,BPF_REG_8 ,32 ), -BPF_JMP_REG (BPF_JSLT ,BPF_REG_1 ,BPF_REG_8 ,16 ), +BPF_JMP_REG (BPF_JSGT ,BPF_REG_1 ,BPF_REG_8 ,16 ),BPF_ALU64_REG (BPF_SUB ,BPF_REG_9 ,BPF_REG_8 ),BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_7 ), BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_8),
@@ -29,7 +29,7 @@BPF_MOV64_REG (BPF_REG_3 ,BPF_REG_2 ),BPF_ALU64_REG (BPF_ADD ,BPF_REG_3 ,BPF_REG_1 ),BPF_MOV64_REG (BPF_REG_1 ,BPF_REG_7 ), -BPF_MOV64_IMM (BPF_REG_5 ,sizeof (struct test_val )), +BPF_MOV64_IMM (BPF_REG_5 ,sizeof (struct test_val )/2 ),BPF_ALU64_REG (BPF_ADD ,BPF_REG_1 ,BPF_REG_5 ),BPF_JMP_REG (BPF_JGE ,BPF_REG_3 ,BPF_REG_1 ,4 ), BPF_MOV64_REG(BPF_REG_1, BPF_REG_6),
Greg Kroah-Hartman
maintainerPATCH 165/166 bpf: Test_progs, add test to catch retval refine error handling
In reply to Greg Kroah-HartmanFrom: John Fastabend <[email protected]>
commit d2db08c7a14e0b5eed6132baf258b80622e041a9 upstream.
Before this series the verifier would clamp return bounds of bpf_get_stack() to [0, X] and this led the verifier to believe that a JMP_JSLT 0 would be false and so would prune that path.
The result is anything hidden behind that JSLT would be unverified. Add a test to catch this case by hiding an goto pc-1 behind the check which will cause an infinite loop if not rejected.
--- tools/testing/selftests/bpf/prog_tests/get_stack_raw_tp.c | 5 ++ tools/testing/selftests/bpf/progs/test_get_stack_rawtp_err.c | 26 +++++++++++ 2 files changed, 31 insertions(+)
tools/testing/selftests/bpf/prog_tests/get_stack_raw_tp.c +5 -0
@@ -82,6 +82,7 @@ static void get_stack_print_output(voidvoid test_get_stack_raw_tp (void ) {const char *file ="./test_get_stack_rawtp.o" ; +const char *file_err ="./test_get_stack_rawtp_err.o" ;const char *prog_name ="raw_tracepoint/sys_enter" ;int i, err, prog_fd, exp_cnt =MAX_CNT_RAWTP ;struct perf_buffer_opts pb_opts = {};
@@ -93,6 +94,10 @@ void test_get_stack_raw_tp(void)struct bpf_map *map;cpu_set_t cpu_set; + err =bpf_prog_load (file_err,BPF_PROG_TYPE_RAW_TRACEPOINT , &obj, &prog_fd); +if (CHECK (err >=0 ,"prog_load raw tp" ,"err %d errno %d\n" , err, errno)) +return ; + err =bpf_prog_load (file,BPF_PROG_TYPE_RAW_TRACEPOINT , &obj, &prog_fd);if (CHECK (err,"prog_load raw tp" ,"err %d errno %d\n" , err, errno))return ;
tools/testing/selftests/bpf/progs/test_get_stack_rawtp_err.c new file +26 -0
@@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> + +#define MAX_STACK_RAWTP 10 + +SEC("raw_tracepoint/sys_enter") +int bpf_prog2 (void *ctx) +{ +__u64 stack[MAX_STACK_RAWTP ]; +int error; + +/* set all the flags which should return -EINVAL */ + error =bpf_get_stack (ctx, stack,0 ,-1 ); +if (error <0 ) + goto loop; + +return error; +loop: +while (1 ) { + error++; + } +} + +char _license[] SEC("license") = "GPL";
Greg Kroah-Hartman
maintainerPATCH 166/166 bpf, test_verifier: switch bpf_get_stacks 0 s> r8 test
In reply to Greg Kroah-HartmanFrom: Daniel Borkmann <[email protected]>
[ no upstream commit ]
Switch the comparison, so that is_branch_taken() will recognize that below branch is never taken:
[...]
17: [...] R1_w=inv0 [...] R8_w=inv(id=0,smin_value=-2147483648,smax_value=-1,umin_value=18446744071562067968,var_off=(0xffffffff80000000; 0x7fffffff)) [...] 17: (67) r8 <<= 32
18: [...] R8_w=inv(id=0,smax_value=-4294967296,umin_value=9223372036854775808,umax_value=18446744069414584320,var_off=(0x8000000000000000; 0x7fffffff00000000)) [...] 18: (c7) r8 s>>= 32
19: [...] R8_w=inv(id=0,smin_value=-2147483648,smax_value=-1,umin_value=18446744071562067968,var_off=(0xffffffff80000000; 0x7fffffff)) [...] 19: (6d) if r1 s> r8 goto pc+16
[...] R1_w=inv0 [...] R8_w=inv(id=0,smin_value=-2147483648,smax_value=-1,umin_value=18446744071562067968,var_off=(0xffffffff80000000; 0x7fffffff)) [...] [...]
Currently we check for is_branch_taken() only if either K is source, or source is a scalar value that is const. For upstream it would be good to extend this properly to check whether dst is const and src not.
For the sake of the test_verifier, it is probably not needed here:
# ./test_verifier 101 #101/p bpf_get_stack return R0 within range OK Summary: 1 PASSED, 0 SKIPPED, 0 FAILED
I haven't seen this issue in test_progs* though, they are passing fine:
# ./test_progs-no_alu32 -t get_stack Switching to flavor 'no_alu32' subdirectory... #20 get_stack_raw_tp:OK Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
# ./test_progs -t get_stack #20 get_stack_raw_tp:OK Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED
--- tools/testing/selftests/bpf/verifier/bpf_get_stack.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/bpf/verifier/bpf_get_stack.c +1 -1
@@ -19,7 +19,7 @@BPF_MOV64_REG (BPF_REG_8 ,BPF_REG_0 ),BPF_ALU64_IMM (BPF_LSH ,BPF_REG_8 ,32 ),BPF_ALU64_IMM (BPF_ARSH ,BPF_REG_8 ,32 ), -BPF_JMP_REG (BPF_JSGT ,BPF_REG_1 ,BPF_REG_8 ,16 ), +BPF_JMP_REG (BPF_JSLT ,BPF_REG_8 ,BPF_REG_1 ,16 ),BPF_ALU64_REG (BPF_SUB ,BPF_REG_9 ,BPF_REG_8 ),BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_7 ), BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_8),
Quoted 8 lines · Greg Kroah-Hartman
This is the start of the stable review cycle for the 5.6.7 release. There are 166 patches in this series, all will be posted as a response to this one. If anyone has any issues with these being applied, please let me know.
Responses should be made by Fri, 24 Apr 2020 09:48:23 +0000. Anything received after that time might be too late.
Build results:
total: 155 pass: 155 fail: 0 Qemu test results: total: 428 pass: 428 fail: 0
Guenter
Greg Kroah-Hartman
maintainerQuoted 6 lines · Guenter Roeck
Quoted 5 lines · Greg Kroah-Hartman
This is the start of the stable review cycle for the 5.6.7 release. There are 166 patches in this series, all will be posted as a response to this one. If anyone has any issues with these being applied, please let me know.
Quoted 8 lines
Quoted 3 lines
Responses should be made by Fri, 24 Apr 2020 09:48:23 +0000. Anything received after that time might be too late.
Build results:
total: 155 pass: 155 fail: 0 Qemu test results: total: 428 pass: 428 fail: 0
Thanks for testing all of these (including the -rc2 versions) and letting me know.
greg k-h
Quoted 10 lines · Greg Kroah-Hartman
This is the start of the stable review cycle for the 5.6.7 release. There are 166 patches in this series, all will be posted as a response to this one. If anyone has any issues with these being applied, please let me know.
Responses should be made by Fri, 24 Apr 2020 09:48:23 +0000. Anything received after that time might be too late.
The whole patch series can be found in one patch at:
Quoted 8 lines
https://www.kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.6.7-rc1.gzor in the git tree and branch at:
git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.gitlinux-5.6.y
and the diffstat can be found below.thanks,
greg k-h
Results from Linaro’s test farm.
No regressions on arm64, arm, x86_64, and i386.
Summary
------------------------------------------------------------------------
kernel: 5.6.7-rc1
git repo: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git
git branch: linux-5.6.y
git commit: 0c5e841761a8a86b28a132964a4418cc9970cc82
git describe: v5.6.6-168-g0c5e841761a8
Test details: https://qa-reports.linaro.org/lkft/linux-stable-rc-5.6-oe/build/v5.6.6-168-g0c5e841761a8
No regressions (compared to build v5.6.6)
No fixes (compared to build v5.6.6)
Ran 36818 total tests in the following environments and test suites.
Environments
--------------
- dragonboard-410c
- hi6220-hikey
- i386
- juno-r2
- juno-r2-compat
- juno-r2-kasan
- nxp-ls2088
- qemu_arm
- qemu_arm64
- qemu_i386
- qemu_x86_64
- x15
- x86
- x86-kasan
Test Suites
-----------
* build
* install-android-platform-tools-r2600
* install-android-platform-tools-r2800
* kselftest
* kselftest/drivers
* kselftest/filesystems
* libgpiod
* linux-log-parser
* ltp-containers-tests
* ltp-ipc-tests
* ltp-sched-tests
* perf
* libhugetlbfs
* ltp-cve-tests
* ltp-dio-tests
* ltp-hugetlb-tests
* ltp-io-tests
* ltp-mm-tests
* ltp-syscalls-tests
* network-basic-tests
* kselftest/net
* kselftest/networking
* kvm-unit-tests
* ltp-cap_bounds-tests
* ltp-commands-tests
* ltp-cpuhotplug-tests
* ltp-crypto-tests
* ltp-fcntl-locktests-tests
* ltp-filecaps-tests
* ltp-fs-tests
* ltp-fs_bind-tests
* ltp-fs_perms_simple-tests
* ltp-fsx-tests
* ltp-math-tests
* ltp-nptl-tests
* ltp-open-posix-tests
* ltp-pty-tests
* ltp-securebits-tests
* v4l2-compliance
* spectre-meltdown-checker-test
* kselftest-vsyscall-mode-native
* kselftest-vsyscall-mode-native/drivers
* kselftest-vsyscall-mode-native/filesystems
* kselftest-vsyscall-mode-native/net
* kselftest-vsyscall-mode-native/networking
* kselftest-vsyscall-mode-none
* kselftest-vsyscall-mode-none/drivers
* kselftest-vsyscall-mode-none/filesystems
* kselftest-vsyscall-mode-none/net
* kselftest-vsyscall-mode-none/networking
Signature · 2 lines
Linaro LKFT https://lkft.linaro.org
Greg Kroah-Hartman
maintainerQuoted 17 lines · Naresh Kamboju
Quoted 15 lines · Greg Kroah-Hartman
This is the start of the stable review cycle for the 5.6.7 release. There are 166 patches in this series, all will be posted as a response to this one. If anyone has any issues with these being applied, please let me know.
Responses should be made by Fri, 24 Apr 2020 09:48:23 +0000. Anything received after that time might be too late.
The whole patch series can be found in one patch at:
https://www.kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.6.7-rc1.gzor in the git tree and branch at:
git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.gitlinux-5.6.y
and the diffstat can be found below.
Quoted 6 lines
Quoted 3 lines
thanks,
greg k-h
Results from Linaro’s test farm.
No regressions on arm64, arm,x86_64, and i386.
Thanks for testing all of these and letting me know.
greg k-h
Jon Hunter
maintainerQuoted 9 lines · Greg Kroah-Hartman
This is the start of the stable review cycle for the 5.6.7 release. There are 166 patches in this series, all will be posted as a response to this one. If anyone has any issues with these being applied, please let me know.
Responses should be made by Fri, 24 Apr 2020 09:48:23 +0000. Anything received after that time might be too late.
The whole patch series can be found in one patch at:
Quoted 8 lines
https://www.kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.6.7-rc1.gzor in the git tree and branch at:
git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.gitlinux-5.6.y
and the diffstat can be found below.thanks,
greg k-h
All tests are passing for Tegra
Test results for stable-v5.6:
13 builds: 13 pass, 0 fail
24 boots: 24 pass, 0 fail
40 tests: 40 pass, 0 failLinux version: 5.6.7-rc1-g8614562dd305
Boards tested: tegra124-jetson-tk1, tegra186-p2771-0000,
tegra194-p2972-0000, tegra20-ventana,
tegra210-p2371-2180, tegra210-p3450-0000,
tegra30-cardhu-a04Cheers
Jon
Signature · 1 line
nvpublic
Greg Kroah-Hartman
maintainerQuoted 27 lines · Jon Hunter
Quoted 9 lines · Greg Kroah-Hartman
This is the start of the stable review cycle for the 5.6.7 release. There are 166 patches in this series, all will be posted as a response to this one. If anyone has any issues with these being applied, please let me know.
Responses should be made by Fri, 24 Apr 2020 09:48:23 +0000. Anything received after that time might be too late.
The whole patch series can be found in one patch at:
Quoted 8 lines
https://www.kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.6.7-rc1.gzor in the git tree and branch at:
git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.gitlinux-5.6.y
and the diffstat can be found below.thanks,
greg k-h
All tests are passing for Tegra
Test results for stable-v5.6:
13 builds: 13 pass, 0 fail 24 boots: 24 pass, 0 fail 40 tests: 40 pass, 0 fail
Quoted 6 lines
Linux version: 5.6.7-rc1-g8614562dd305
Boards tested: tegra124-jetson-tk1, tegra186-p2771-0000,tegra194-p2972-0000, tegra20-ventana, tegra210-p2371-2180, tegra210-p3450-0000, tegra30-cardhu-a04
Great, thanks for testing all of these and letting me know.
greg k-h
Quoted 9 lines · Greg Kroah-Hartman
This is the start of the stable review cycle for the 5.6.7 release. There are 166 patches in this series, all will be posted as a response to this one. If anyone has any issues with these being applied, please let me know.
Responses should be made by Fri, 24 Apr 2020 09:48:23 +0000. Anything received after that time might be too late.
The whole patch series can be found in one patch at:
Quoted 9 lines
https://www.kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.6.7-rc1.gzor in the git tree and branch at:
git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.gitlinux-5.6.y
and the diffstat can be found below.thanks,
greg k-h
Compiled and booted on my test system. No dmesg regressions.
thanks,
-- Shuah
Greg Kroah-Hartman
maintainerQuoted 15 lines · shuah
Quoted 14 lines · Greg Kroah-Hartman
This is the start of the stable review cycle for the 5.6.7 release. There are 166 patches in this series, all will be posted as a response to this one. If anyone has any issues with these being applied, please let me know.
Responses should be made by Fri, 24 Apr 2020 09:48:23 +0000. Anything received after that time might be too late.
The whole patch series can be found in one patch at:
https://www.kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.6.7-rc1.gzor in the git tree and branch at:
git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.gitlinux-5.6.y
and the diffstat can be found below.
Quoted 6 lines
Quoted 4 lines
thanks,
greg k-h
Compiled and booted on my test system. No dmesg regressions.
Thanks for testing all of these and letting me know.
greg k-h