Skip to content

Commit 6aef78a

Browse files
committed
copystr(9): Move to deprecate (attempt #2)
This reapplies logical r360944 and r360946 (reverting r360955), with fixed copystr() stand-in replacement macro. Eventually the goal is to convert consumers and kill the macro, but for a first step it helps if the macro is correct. Prior commit message: Unlike the other copy*() functions, it does not serve to copy from one address space to another or protect against potential faults. It's just an older incarnation of the now-more-common strlcpy(). Add a coccinelle script to tools/ which can be used to mechanically convert existing instances where replacement with strlcpy is trivial. In the two cases which matched, fuse_vfsops.c and union_vfsops.c, the code was further refactored manually to simplify. Replace the declaration of copystr() in systm.h with a small macro wrapper around strlcpy (with correction from brooks@ -- thanks). Remove N redundant MI implementations of copystr. For MIPS, this entailed inlining the assembler copystr into the only consumer, copyinstr, and making the latter a leaf function. Reviewed by: jhb (earlier version) Discussed with: brooks (thanks!) Differential Revision: https://reviews.freebsd.org/D24672
1 parent 8b7082f commit 6aef78a

File tree

15 files changed

+75
-362
lines changed

15 files changed

+75
-362
lines changed

sys/amd64/amd64/support.S

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,43 +1416,6 @@ copyinstr_toolong:
14161416
movl $ENAMETOOLONG,%eax
14171417
jmp cpystrflt_x
14181418

1419-
/*
1420-
* copystr(from, to, maxlen, int *lencopied)
1421-
* %rdi, %rsi, %rdx, %rcx
1422-
*/
1423-
ENTRY(copystr)
1424-
PUSH_FRAME_POINTER
1425-
movq %rdx,%r8 /* %r8 = maxlen */
1426-
1427-
incq %rdx
1428-
1:
1429-
decq %rdx
1430-
jz 4f
1431-
movb (%rdi),%al
1432-
movb %al,(%rsi)
1433-
incq %rsi
1434-
incq %rdi
1435-
testb %al,%al
1436-
jnz 1b
1437-
1438-
/* Success -- 0 byte reached */
1439-
decq %rdx
1440-
xorl %eax,%eax
1441-
2:
1442-
testq %rcx,%rcx
1443-
jz 3f
1444-
/* set *lencopied and return %rax */
1445-
subq %rdx,%r8
1446-
movq %r8,(%rcx)
1447-
3:
1448-
POP_FRAME_POINTER
1449-
ret
1450-
4:
1451-
/* rdx is zero -- return ENAMETOOLONG */
1452-
movl $ENAMETOOLONG,%eax
1453-
jmp 2b
1454-
END(copystr)
1455-
14561419
/*
14571420
* Handling of special amd64 registers and descriptor tables etc
14581421
*/

sys/arm/arm/copystr.S

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,39 +60,6 @@ __FBSDID("$FreeBSD$");
6060
ldr tmp, .Lpcb
6161
#endif
6262

63-
/*
64-
* r0 - from
65-
* r1 - to
66-
* r2 - maxlens
67-
* r3 - lencopied
68-
*
69-
* Copy string from r0 to r1
70-
*/
71-
ENTRY(copystr)
72-
stmfd sp!, {r4-r5} /* stack is 8 byte aligned */
73-
teq r2, #0x00000000
74-
mov r5, #0x00000000
75-
moveq r0, #ENAMETOOLONG
76-
beq 2f
77-
78-
1: ldrb r4, [r0], #0x0001
79-
add r5, r5, #0x00000001
80-
teq r4, #0x00000000
81-
strb r4, [r1], #0x0001
82-
teqne r5, r2
83-
bne 1b
84-
85-
teq r4, #0x00000000
86-
moveq r0, #0x00000000
87-
movne r0, #ENAMETOOLONG
88-
89-
2: teq r3, #0x00000000
90-
strne r5, [r3]
91-
92-
ldmfd sp!, {r4-r5} /* stack is 8 byte aligned */
93-
RET
94-
END(copystr)
95-
9663
#define SAVE_REGS stmfd sp!, {r4-r6}
9764
#define RESTORE_REGS ldmfd sp!, {r4-r6}
9865

sys/arm64/arm64/copystr.c

Lines changed: 0 additions & 61 deletions
This file was deleted.

sys/conf/files.arm64

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ arm64/arm64/busdma_machdep.c standard
134134
arm64/arm64/bzero.S standard
135135
arm64/arm64/clock.c standard
136136
arm64/arm64/copyinout.S standard
137-
arm64/arm64/copystr.c standard
138137
arm64/arm64/cpu_errata.c standard
139138
arm64/arm64/cpufunc_asm.S standard
140139
arm64/arm64/db_disasm.c optional ddb

sys/conf/files.powerpc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ powerpc/powerpc/bus_machdep.c standard
241241
powerpc/powerpc/busdma_machdep.c standard
242242
powerpc/powerpc/clock.c standard
243243
powerpc/powerpc/copyinout.c standard
244-
powerpc/powerpc/copystr.c standard
245244
powerpc/powerpc/cpu.c standard
246245
powerpc/powerpc/cpu_subr64.S optional powerpc64
247246
powerpc/powerpc/db_disasm.c optional ddb

sys/conf/files.riscv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ riscv/riscv/busdma_bounce.c standard
3737
riscv/riscv/busdma_machdep.c standard
3838
riscv/riscv/clock.c standard
3939
riscv/riscv/copyinout.S standard
40-
riscv/riscv/copystr.c standard
4140
riscv/riscv/cpufunc_asm.S standard
4241
riscv/riscv/db_disasm.c optional ddb
4342
riscv/riscv/db_interface.c optional ddb

sys/fs/fuse/fuse_vfsops.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,6 @@ fuse_vfsop_mount(struct mount *mp)
303303
int daemon_timeout;
304304
int fd;
305305

306-
size_t len;
307-
308306
struct cdev *fdev;
309307
struct fuse_data *data = NULL;
310308
struct thread *td;
@@ -437,8 +435,8 @@ fuse_vfsop_mount(struct mount *mp)
437435
strlcat(mp->mnt_stat.f_fstypename, ".", MFSNAMELEN);
438436
strlcat(mp->mnt_stat.f_fstypename, subtype, MFSNAMELEN);
439437
}
440-
copystr(fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &len);
441-
bzero(mp->mnt_stat.f_mntfromname + len, MNAMELEN - len);
438+
memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
439+
strlcpy(mp->mnt_stat.f_mntfromname, fspec, MNAMELEN);
442440
mp->mnt_iosize_max = MAXPHYS;
443441

444442
/* Now handshaking with daemon */

sys/fs/unionfs/union_vfsops.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ unionfs_domount(struct mount *mp)
8383
char *tmp;
8484
char *ep;
8585
int len;
86-
size_t done;
8786
int below;
8887
uid_t uid;
8988
gid_t gid;
@@ -304,12 +303,8 @@ unionfs_domount(struct mount *mp)
304303
*/
305304
vfs_getnewfsid(mp);
306305

307-
len = MNAMELEN - 1;
308-
tmp = mp->mnt_stat.f_mntfromname;
309-
copystr((below ? "<below>:" : "<above>:"), tmp, len, &done);
310-
len -= done - 1;
311-
tmp += done - 1;
312-
copystr(target, tmp, len, NULL);
306+
snprintf(mp->mnt_stat.f_mntfromname, MNAMELEN, "<%s>:%s",
307+
below ? "below" : "above", target);
313308

314309
UNIONFSDEBUG("unionfs_mount: from %s, on %s\n",
315310
mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);

sys/i386/i386/support.s

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -233,47 +233,6 @@ ENTRY(memcpy)
233233
ret
234234
END(memcpy)
235235

236-
/*
237-
* copystr(from, to, maxlen, int *lencopied) - MP SAFE
238-
*/
239-
ENTRY(copystr)
240-
pushl %esi
241-
pushl %edi
242-
243-
movl 12(%esp),%esi /* %esi = from */
244-
movl 16(%esp),%edi /* %edi = to */
245-
movl 20(%esp),%edx /* %edx = maxlen */
246-
incl %edx
247-
1:
248-
decl %edx
249-
jz 4f
250-
lodsb
251-
stosb
252-
orb %al,%al
253-
jnz 1b
254-
255-
/* Success -- 0 byte reached */
256-
decl %edx
257-
xorl %eax,%eax
258-
jmp 6f
259-
4:
260-
/* edx is zero -- return ENAMETOOLONG */
261-
movl $ENAMETOOLONG,%eax
262-
263-
6:
264-
/* set *lencopied and return %eax */
265-
movl 20(%esp),%ecx
266-
subl %edx,%ecx
267-
movl 24(%esp),%edx
268-
testl %edx,%edx
269-
jz 7f
270-
movl %ecx,(%edx)
271-
7:
272-
popl %edi
273-
popl %esi
274-
ret
275-
END(copystr)
276-
277236
ENTRY(bcmp)
278237
pushl %edi
279238
pushl %esi

sys/kern/subr_csan.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -350,20 +350,12 @@ kcsan_strlen(const char *str)
350350
return (s - str);
351351
}
352352

353-
#undef copystr
354353
#undef copyin
355354
#undef copyin_nofault
356355
#undef copyinstr
357356
#undef copyout
358357
#undef copyout_nofault
359358

360-
int
361-
kcsan_copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
362-
{
363-
kcsan_access((uintptr_t)kdaddr, len, true, false, __RET_ADDR);
364-
return copystr(kfaddr, kdaddr, len, done);
365-
}
366-
367359
int
368360
kcsan_copyin(const void *uaddr, void *kaddr, size_t len)
369361
{

sys/mips/mips/support.S

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,22 @@
105105
.text
106106

107107
/*
108-
* int copystr(void *kfaddr, void *kdaddr, size_t maxlen, size_t *lencopied)
109-
* Copy a NIL-terminated string, at most maxlen characters long. Return the
110-
* number of characters copied (including the NIL) in *lencopied. If the
111-
* string is too long, return ENAMETOOLONG; else return 0.
108+
* Copy a null terminated string from the user address space into
109+
* the kernel address space.
110+
*
111+
* copyinstr(fromaddr, toaddr, maxlength, &lencopied)
112+
* caddr_t fromaddr;
113+
* caddr_t toaddr;
114+
* u_int maxlength;
115+
* u_int *lencopied;
112116
*/
113-
LEAF(copystr)
117+
LEAF(copyinstr)
118+
PTR_LA v0, __copyinstr_err
119+
blt a0, zero, __copyinstr_err # make sure address is in user space
120+
GET_CPU_PCPU(v1)
121+
PTR_L v1, PC_CURPCB(v1)
122+
PTR_S v0, U_PCB_ONFAULT(v1)
123+
114124
move t0, a2
115125
beq a2, zero, 4f
116126
1:
@@ -128,37 +138,14 @@ LEAF(copystr)
128138
PTR_SUBU a2, t0, a2 # if the 4th arg was non-NULL
129139
PTR_S a2, 0(a3)
130140
3:
131-
j ra # v0 is 0 or ENAMETOOLONG
132-
nop
133-
END(copystr)
134141

142+
PTR_S zero, U_PCB_ONFAULT(v1)
143+
j ra
144+
nop
135145

136-
/*
137-
* Copy a null terminated string from the user address space into
138-
* the kernel address space.
139-
*
140-
* copyinstr(fromaddr, toaddr, maxlength, &lencopied)
141-
* caddr_t fromaddr;
142-
* caddr_t toaddr;
143-
* u_int maxlength;
144-
* u_int *lencopied;
145-
*/
146-
NESTED(copyinstr, CALLFRAME_SIZ, ra)
147-
PTR_SUBU sp, sp, CALLFRAME_SIZ
148-
.mask 0x80000000, (CALLFRAME_RA - CALLFRAME_SIZ)
149-
PTR_LA v0, copyerr
150-
blt a0, zero, _C_LABEL(copyerr) # make sure address is in user space
151-
REG_S ra, CALLFRAME_RA(sp)
152-
GET_CPU_PCPU(v1)
153-
PTR_L v1, PC_CURPCB(v1)
154-
jal _C_LABEL(copystr)
155-
PTR_S v0, U_PCB_ONFAULT(v1)
156-
REG_L ra, CALLFRAME_RA(sp)
157-
GET_CPU_PCPU(v1)
158-
PTR_L v1, PC_CURPCB(v1)
159-
PTR_S zero, U_PCB_ONFAULT(v1)
160-
j ra
161-
PTR_ADDU sp, sp, CALLFRAME_SIZ
146+
__copyinstr_err:
147+
j ra
148+
li v0, EFAULT
162149
END(copyinstr)
163150

164151
/*

0 commit comments

Comments
 (0)