Kernel Newbies archive on lore.kernel.org
 help / color / Atom feed
* Dubios pointer casting with put_user()
@ 2020-07-17  0:13 Richard Sailer
  2020-07-17  0:19 ` Richard Sailer
  2020-07-17 22:46 ` Valdis Klētnieks
  0 siblings, 2 replies; 5+ messages in thread
From: Richard Sailer @ 2020-07-17  0:13 UTC (permalink / raw)
  To: kernelnewbies

[-- Attachment #1.1.1: Type: text/plain, Size: 803 bytes --]

Hi,

In the following example from net/dccp/proto.c the pointer given
put_user() is casted to (int __user *) although the value to copy is a
unsigned long. Is this (correctness and security wise) sane? Because as
I understand it put_user() determines the amount it copies from the
pointer type.

```
		unsigned long amount = 0;

		skb = skb_peek(&sk->sk_receive_queue);
		if (skb != NULL) {
			amount = skb->len;
		}
		rc = put_user(amount, (int __user *)arg);
```

Also skb->len is an unsigned int realisticly in most cases < 9000 (and
in all cases I can imagine < int_max (with 16 bit)).

I would like to declare amount as int outside of the switch case
statement (because I  need it in another case statement as signed int)
would it be safe to do so?

Thanks,
-- Richard


[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Dubios pointer casting with put_user()
  2020-07-17  0:13 Dubios pointer casting with put_user() Richard Sailer
@ 2020-07-17  0:19 ` Richard Sailer
  2020-07-17 22:46 ` Valdis Klētnieks
  1 sibling, 0 replies; 5+ messages in thread
From: Richard Sailer @ 2020-07-17  0:19 UTC (permalink / raw)
  To: kernelnewbies

[-- Attachment #1.1.1: Type: text/plain, Size: 94 bytes --]

Link to full source:
https://elixir.bootlin.com/linux/latest/source/net/dccp/proto.c#L390


[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Dubios pointer casting with put_user()
  2020-07-17  0:13 Dubios pointer casting with put_user() Richard Sailer
  2020-07-17  0:19 ` Richard Sailer
@ 2020-07-17 22:46 ` Valdis Klētnieks
  2020-07-19 12:15   ` Richard Sailer
  1 sibling, 1 reply; 5+ messages in thread
From: Valdis Klētnieks @ 2020-07-17 22:46 UTC (permalink / raw)
  To: Richard Sailer; +Cc: kernelnewbies

[-- Attachment #1.1: Type: text/plain, Size: 337 bytes --]

On Fri, 17 Jul 2020 02:13:34 +0200, Richard Sailer said:

> unsigned long. Is this (correctness and security wise) sane? Because as
> I understand it put_user() determines the amount it copies from the
> pointer type.

> 		rc = put_user(amount, (int __user *)arg);

If that were true, you wouldn't need to pass the 'amount' variable....

[-- Attachment #1.2: Type: application/pgp-signature, Size: 832 bytes --]

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Dubios pointer casting with put_user()
  2020-07-17 22:46 ` Valdis Klētnieks
@ 2020-07-19 12:15   ` Richard Sailer
  2020-07-19 22:50     ` Valdis Klētnieks
  0 siblings, 1 reply; 5+ messages in thread
From: Richard Sailer @ 2020-07-19 12:15 UTC (permalink / raw)
  To: Valdis Klētnieks; +Cc: kernelnewbies

[-- Attachment #1.1.1: Type: text/plain, Size: 1272 bytes --]



On 18/07/2020 00:46, Valdis Klētnieks wrote:
> On Fri, 17 Jul 2020 02:13:34 +0200, Richard Sailer said:
> 
>> unsigned long. Is this (correctness and security wise) sane? Because as
>> I understand it put_user() determines the amount it copies from the
>> pointer type.
> 
>> 		rc = put_user(amount, (int __user *)arg);
> 
> If that were true, you wouldn't need to pass the 'amount' variable....
> 
Hmm, that would make no sense to me. arg is a pointer to user space
memory, put_user would still need the value to copy to that memory.

And my understanding of put_user() comes from its definition in uaccess.h:

#define put_user(x, ptr)					\
({								\
	int __ret_pu;						\
	__typeof__(*(ptr)) __pu_val;				\
	__chk_user_ptr(ptr);					\
	might_fault();						\
	__pu_val = x;						\
	switch (sizeof(*(ptr))) {				\
	case 1:							\
		__put_user_x(1, __pu_val, ptr, __ret_pu);	\
		break;						\
	case 2:							\
		__put_user_x(2, __pu_val, ptr, __ret_pu);	\
		break;						\
	case 4:							\
		__put_user_x(4, __pu_val, ptr, __ret_pu);	\
		break;						\
	case 8:							\
		__put_user_x8(__pu_val, ptr, __ret_pu);		\
		break;						\
[...]

But please tell me if I got anything wrong here, I'm still not 100% sure

-- Richard


[-- Attachment #1.2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Dubios pointer casting with put_user()
  2020-07-19 12:15   ` Richard Sailer
@ 2020-07-19 22:50     ` Valdis Klētnieks
  0 siblings, 0 replies; 5+ messages in thread
From: Valdis Klētnieks @ 2020-07-19 22:50 UTC (permalink / raw)
  To: Richard Sailer; +Cc: kernelnewbies

[-- Attachment #1.1: Type: text/plain, Size: 422 bytes --]

On Sun, 19 Jul 2020 14:15:36 +0200, Richard Sailer said:

> > If that were true, you wouldn't need to pass the 'amount' variable....
> >
> Hmm, that would make no sense to me. arg is a pointer to user space
> memory, put_user would still need the value to copy to that memory.

-ENOCAFFEINE. :)

I was thinking copy_(to/from)_user, not get/put.

I will now depart for the grocery store and obtain a supply of caffeine :)


[-- Attachment #1.2: Type: application/pgp-signature, Size: 832 bytes --]

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, back to index

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-17  0:13 Dubios pointer casting with put_user() Richard Sailer
2020-07-17  0:19 ` Richard Sailer
2020-07-17 22:46 ` Valdis Klētnieks
2020-07-19 12:15   ` Richard Sailer
2020-07-19 22:50     ` Valdis Klētnieks

Kernel Newbies archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/kernelnewbies/0 kernelnewbies/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 kernelnewbies kernelnewbies/ https://lore.kernel.org/kernelnewbies \
		kernelnewbies@kernelnewbies.org
	public-inbox-index kernelnewbies

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernelnewbies.kernelnewbies


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git